Ich bin neu auf Android und versuchen, folgende Aufgabe zu tun: - Ich möchte WhatsApp Kontaktliste öffnen und ausgewählte Kontaktdaten. Ich kann die Hälfte der Arbeit leisten. Die WhatsApp-Kontaktliste öffnet sich, aber sobald ich auf einen Kontakt drücke, stürzt meine App ab. Hier ist mein Code:Open WhatsApp Kontakte Liste von Android-App
Um WhatsApp Kontaktliste zu öffnen:
btnChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setPackage("com.whatsapp");
try{
Toast.makeText(AddScheduleActivity.this, "going out activity", Toast.LENGTH_SHORT).show();
startActivityForResult(intent, 1);
Toast.makeText(AddScheduleActivity.this, "getting In activity", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(AddScheduleActivity.this, "Whatsapp not found", Toast.LENGTH_SHORT).show(); //no activity found to handle this intent means whatsapp is not installed
}
}
});
Auswahl erhalten: -
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Toast.makeText(AddScheduleActivity.this, "Ok In", Toast.LENGTH_SHORT).show();
switch (requestCode) {
case 1:
if(resultCode == RESULT_OK){
if(intent.hasExtra("contact")){
Toast.makeText(AddScheduleActivity.this, "Ok working", Toast.LENGTH_SHORT).show();
Uri uri = intent.getData();
//Query the content uri
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// column index of the phone number
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// column index of the contact name
int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
contactNumber = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
}
}
break;
default:
break;
}
}
Wenn ich wählen Sie keinen Kontakt und drücken Sie zurück, ihr kein Absturz in diesem Fall
Vielen Dank im Voraus.
Wenn die Anwendung abstürzt, sind die logcat in Ihrer Frage –
http://stackoverflow.com/questions/24276573/how-to-get-contacts-which-are-used-in -whatsapp-oder-andere-Anwendung-in-Android Bereits jetzt wird eine ähnliche Frage gestellt. Prüfen Sie, ob dies für Sie funktioniert. – eshb
@baymaxx Danke, aber ich habe es schon gesehen, es ist nicht hilfreich – Priyanka