7

Hallo Ich möchte einen Kontakt aus unserer Standard-Kontaktbuchabsicht auswählen. Ich habe verschiedene Möglichkeiten ausprobiert. Bitte finden Sie den Code unten. Das Problem mit all diesen Codes ist, dass sie einen Bildschirm für Zwischendokumente mit wenigen Optionen öffnen, wo der Benutzer den Kontakt auswählen muss und dann das Kontaktbuch öffnet.Wählen Sie den Kontakt direkt aus der Kontaktauswahl Absicht

private void openContactIntent() { 
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT, ContactsContract.Contacts.CONTENT_URI); 
    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 
    startActivityForResult(intent, REQ_CONTACT_DIRECTORY); 
} 

Ich habe auch versucht

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
startActivityForResult(intent, PICK_CONTACT); 

und

Intent intent = new Intent(Intent.ACTION_PICK); 
intent.setType(ContactsContract.Contacts.CONTENT_TYPE); 
startActivityForResult(intent, PICK_CONTACT); 

Was ich als Zwischen Bildschirm zu sehen ist enter image description here

+0

'Intent Absicht = new Intent (Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI) ; startActivityForResult (Absicht, PICK_CONTACT); 'Es funktioniert für mich! –

+0

Ist es mit einem Betriebssystem verbunden? Ich führe Code auf Android N. Und für mich funktioniert es nicht. Ich habe keine Berechtigungen hinzugefügt. –

+0

Ich betreibe auch Android N! –

Antwort

0

Dies funktioniert für mich:

Intent it= new Intent(Intent.ACTION_PICK, 
    ContactsContract.Contacts.CONTENT_URI); 
startActivityForResult(it, requestCode); 
2

den Code unten versuchen Kontakt aufzunehmen:

Intent contactPickerIntent = new Intent (Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI); startActivityForResult (contactPickerIntent, RESULT_PICK_CONTACT);

Sie können die erforderlichen Informationen in onActivityResult holen wie folgt:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     switch (requestCode) { 
      case RESULT_PICK_CONTACT: 
        Cursor cursor = null; 
    try { 
     String phoneNo = null; 
     String name = null; 

     Uri uri = data.getData(); 
     cursor = getContentResolver().query(uri, null, null, null, null); 
     cursor.moveToFirst(); 
     int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
     int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
     phoneNo = cursor.getString(phoneIndex); 
     name = cursor.getString(nameIndex); 

     Log.e("Name and Contact number is",name+","+phoneNo); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
       break; 
     } 
    } else { 
     Log.e("Failed", "Not able to pick contact"); 
    } 
} 
+0

Diese Lösung hat für mich funktioniert! –

+0

oH .. !! Es ist großartig..!! –

5

Ich hatte auch das gleiche Problem. Schließlich wurde ich von Zwischen Picker Bildschirm los Code unten verwenden,

Intent i=new Intent(Intent.ACTION_PICK); 
i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); 
startActivityForResult(i, SELECT_PHONE_NUMBER); 

In onActivityResult get Telefonnummer wie unten

if (requestCode == SELECT_PHONE_NUMBER && resultCode == RESULT_OK) { 
    // Get the URI and query the content provider for the phone number 
    Uri contactUri = data.getData(); 
    String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}; 
    Cursor cursor = getContext().getContentResolver().query(contactUri, projection, 
     null, null, null); 

    // If the cursor returned is valid, get the phone number 
    if (cursor != null && cursor.moveToFirst()) { 
    int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
    String number = cursor.getString(numberIndex); 
    // Do something with the phone number 
    ... 
    } 

    cursor.close(); 
} 
Verwandte Themen