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

Har*_*edi 11 android android-intent android-contacts android-implicit-intent contactpicker

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

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);
}
Run Code Online (Sandbox Code Playgroud)

我也试过了

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
Run Code Online (Sandbox Code Playgroud)

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT); 
Run Code Online (Sandbox Code Playgroud)

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

sch*_*ill 11

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

Intent i=new Intent(Intent.ACTION_PICK);
i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(i, SELECT_PHONE_NUMBER);
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)


Pha*_*inh 7

这是一种从 Kotlin 中的联系人中选择姓名、电话号码的方法

private fun pickEmergencyFromContacts() {
    val i = Intent(Intent.ACTION_PICK)
    i.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
    startActivityForResult(i, SELECT_PHONE_NUMBER)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == SELECT_PHONE_NUMBER && resultCode == Activity.RESULT_OK) {
        val contactUri = data?.data ?: return
        val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER)
        val cursor = requireContext().contentResolver.query(contactUri, projection,
                null, null, null)

        if (cursor != null && cursor.moveToFirst()) {
            val nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
            val numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
            val name = cursor.getString(nameIndex)
            val number = cursor.getString(numberIndex)

            // do something with name and phone
        }
        cursor?.close()
    }
}

companion object {

    private const val SELECT_PHONE_NUMBER = 111    
    ...
}
Run Code Online (Sandbox Code Playgroud)

希望有帮助


And*_*eek 5

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

Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
Run Code Online (Sandbox Code Playgroud)

您可以在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");
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 1

这对我有用:

Intent it= new Intent(Intent.ACTION_PICK, 
     ContactsContract.Contacts.CONTENT_URI); 
startActivityForResult(it, requestCode);
Run Code Online (Sandbox Code Playgroud)