Skip to content Skip to sidebar Skip to footer

How To Import A Specific Contact's Phone Number?

I'm trying to Read Phone number of a Contact Selected using Contact Picker. The Display Name works fine, But Phone number doesn't. Code: //calling Contact Picker public void CPick(

Solution 1:

If you want to allow the user to pick a phone-number, the best option is to use a PHONE-PICKER not a CONTACT-PICKER:

Intentintent=newIntent(Intent.ACTION_PICK, CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_PHONE);

...

protectedvoidonActivityResult(int requestCode, int resultCode, Intent intent){
    if (requestCode == PICK_PHONE && resultCode == RESULT_OK){
        UriphoneUri= intent.getData();
        Cursorcur= getContentResolver().query(phoneUri, newString[] { Phone.DISPLAY_NAME, Phone.NUMBER }, null, null, null);
        if (cur != null && cur.moveToFirst()){
            Stringname= cur.getString(0);
            Stringnumber= cur.getString(1);
            Log.d("PHONE-PICKER", "User picker: " + name + " - " + number);
            cur.close();
        }
    }
}

Solution 2:

Try this method:

privatevoidretrieveContactNumber() {

  String contactNumber = null;

  // getting contacts IDCursor cursorID = getContentResolver().query(uriContact,
        newString[]{ContactsContract.Contacts._ID},
        null, null, null);

  if (cursorID.moveToFirst()) {

    contactId = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
  }

  cursorID.close();

  Log.d(TAG, "Contact ID: " + contactId);

  // Using the contact ID now we will get contact phone numberCursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        newString[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

        newString[]{contactId}, null);

  if (cursorPhone.moveToFirst()) {
    contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  }

  cursorPhone.close();

  Log.d(TAG, "Contact Phone Number: " + contactNumber);
}

You should see the contact number in your logcat.

Post a Comment for "How To Import A Specific Contact's Phone Number?"