Pick contact directly from contact picker intent

随声附和 提交于 2019-11-29 06:15:05

I also had the same problem. Finally, I got rid of intermediate picker screen using below code,

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

In onActivityResult get phone number as below

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

Try the below code to pick contact:

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

You can fetch the required information in onActivityResult as follows:

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

This works for me:

Intent it= new Intent(Intent.ACTION_PICK, 
     ContactsContract.Contacts.CONTENT_URI); 
startActivityForResult(it, requestCode);
 private static final int RESULT_PICK_CONTACT1= 1;
 public void pickContact1 (View v)
      {
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
        startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT1);
      }
 @Override
    protected void onActivityResult ( int requestCode, int resultCode, Intent data){
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case RESULT_PICK_CONTACT1:
                    contactPicked1(data);
                    break;
            }
        } else {
            Log.e("SetupActivity", "Failed to pick contact");
        }
    }

 private void contactPicked1 (Intent data){
        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);
            name = cursor.getString(nameIndex);
            phoneNo = cursor.getString(phoneIndex);
            ed11.setText(name);
            ed12.setText(phoneNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

This will surely work.

Here is a way to pick name, phone number from Contact in 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    
    ...
}

Hope it help

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!