直接从联系人选择器意图中选择联系人

您好我想从我们的默认联系簿意图中选择一个联系人。 我尝试了几种方法来做到这一点。 请在下面find代码。 所有这些代码的问题在于,他们打开一个中间文档屏幕,用户必须选择联系人,而不是打开联系簿。

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

我也试过了

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

我所看到的中间屏幕是 在此处输入图像描述

    我也有同样的问题。 最后,我使用下面的代码摆脱了中间选择器屏幕,

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

    onActivityResult获取如下电话号码

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

    请尝试以下代码来选择联系人:

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

    您可以在onActivityResult中获取所需信息,如下所示:

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

    这对我有用:

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