1

I've been googl'ed around alot to find out how I can get the phone number when the user choose a contact in the contact picker. I know how to open the contact picker. But not how to get the phone number.

I've tried these examples:

http://mobile.tutsplus.com/tutorials/android/android-essentials-using-the-contact-picker/
http://www.enkeladress.com/article.php/android_snippen_show_contact_picker

And alot of stackoverflow threads, but the Phone class seem's to be deprecated. So, how can I do it?

Thanks in advance!

(Really sorry for bad english! Hope you understand!)

GuiceU
  • 1,030
  • 6
  • 19
  • 35
  • Have you looked at ContactsContract? It replaced the deprecated Phone http://developer.android.com/reference/android/provider/ContactsContract.Contacts.html – barbiepylon Jul 12 '12 at 13:10
  • Useful link: http://stackoverflow.com/questions/3044545/get-contact-info-from-android-contact-picker – Fran Verona Jul 12 '12 at 13:10
  • Here you go http://stackoverflow.com/questions/8612531/how-can-i-choose-a-phone-number-with-androids-contacts-dialog – Vipul Jul 12 '12 at 13:11
  • Thanks dudes! Now I got it work :) Thank you very much! – GuiceU Jul 12 '12 at 15:15
  • I think I got everýthing work except one thing, I cant replace the Phone.DATA? I've tried with ContactsContract.Data and ContactsContract.Contacts.Data? Plese help me – GuiceU Jul 12 '12 at 15:36
  • The code here from bizar0 shows how to get the phone number. http://stackoverflow.com/questions/866769/how-to-call-android-contacts-list – userman Sep 02 '12 at 08:41

1 Answers1

2

This worked for me.

Uri contactData = data.getData();
Cursor c =  main.managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
    String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
}

Cursor c1 = mcontext.getContentResolver().query(Data.CONTENT_URI,
    new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
    Data.CONTACT_ID + "=?" + " AND "
    + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
    new String[] {String.valueOf(contactId)}, null);
c1.moveToFirst();
String number = c1.getString(1);

I got the query from the google documentation. You get string at position one because that's the position in the query above.
http://developer.android.com/reference/android/provider/ContactsContract.Data.html

userman
  • 301
  • 2
  • 4