Reputation: 1038
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!)
Upvotes: 1
Views: 4856
Reputation: 301
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
Upvotes: 2