7

Xin chào Tôi muốn chọn một liên hệ từ mục đích liên hệ mặc định của cuốn sách liên hệ của chúng tôi. Tôi đã thử một số cách để làm điều đó. Vui lòng tìm mã bên dưới. Vấn đề với tất cả các mã đó là họ mở một màn hình tài liệu trung gian với một vài tùy chọn ở đó người dùng phải chọn liên hệ và hơn là mở sổ liên lạc.Chọn liên hệ trực tiếp từ ý định của người liên hệ liên hệ

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); 
} 

Tôi cũng đã cố gắng

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

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

Những gì tôi thấy là một màn hình trung gian là enter image description here

+0

'ý định Ý định = Ý định mới (Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI) ; startActivityForResult (intent, PICK_CONTACT); 'Nó làm việc cho tôi! –

+0

Có liên quan đến bất kỳ hệ điều hành nào không? Tôi đang chạy mã trên Android N. Và đối với tôi nó không hoạt động. Tôi chưa thêm bất kỳ quyền nào. –

+0

Tôi cũng đang chạy Android N! –

Trả lời

0

này làm việc cho tôi:

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

Hãy thử đoạn code dưới đây để chọn liên hệ:

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

Bạn có thể lấy các thông tin cần thiết trong onActivityResult như sau:

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

Giải pháp này phù hợp với tôi! –

+0

oH .. !! Thật tuyệt vời..!! –

5

Tôi cũng có cùng một vấn đề. Cuối cùng, tôi đã thoát khỏi màn hình chọn trung gian sử dụng bên dưới mã,

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

Trong onActivityResult số get điện thoại như sau

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(); 
} 
Các vấn đề liên quan