I use this code to make the system contact picker show only those contacts
which have email addresses:
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
intent.setType(Email.CONTENT_TYPE);
Just checked that this works too (only contacts with phone numbers are
shown):
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
intent.setType(Phone.CONTENT_TYPE);
This type of filtering seems to be available starting with Android 3.2, and
works on stock Android and manufacturer specific versions (like Samsung,
Sony, ...)
Still, I recommend an additional check beforehand with
PackageManager.queryIntentActivities, and having a catch block for
ActivityNotFoundException.
As for getting the picked phone number:
In the activity result, you'll have a Uri that looks like <contacts
authority>"contacts/lookup/*/#" (when filtering is not available, and the
user picked a contact -- then you'll need to query its list of phone
numbers, there may not be any) or "data/#" (when filtering is available,
and the user picked a specific phone number).
-- K
On Sunday, November 17, 2013 5:06:07 PM UTC+4, mbanzon wrote:
>
> Hi all,
>
> I am trying to get a contact with a phone number and so far I haven't
> found a reliable way to do this. I launch the standard activity for picking
> a contact using this snippet:
>
> Intent intent = new Intent(Intent.ACTION_PICK,
> ContactsContract.Contacts.CONTENT_URI);
> startActivityForResult(intent, REQUEST_CONTACT_NUMBER);
>
> In my onActivityResult method I do this (from examples from SO etc.):
>
> if (REQUEST_CONTACT_NUMBER == requestCode) {
> if (resultCode == Activity.RESULT_OK) {
> Uri contactData = data.getData();
> Cursor c = getContentResolver().query(contactData, null, null, null, null);
> if (c.moveToFirst()) {
> String id =
> c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
> String hasPhone =
> c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
>
> if (hasPhone.equalsIgnoreCase("1")) {
> Cursor phones = getContentResolver().query(
> ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
> ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
> null, null);
> if (phones.moveToFirst()) {
> String number =
> phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
> this.numberEditText.setText(number);
> return;
> }
> }
> }
> }
> Toast.makeText(this, "No number", Toast.LENGTH_SHORT).show();
> }
>
> The activity that launches show a lot more contacts than expected - every
> person is shown once per phone number or email eg. - many of the contacts
> is shown three to five times - and only one of these will result in an
> actual phone number being returned. Some contacts even return the email
> address!
>
> If I launch the activity using this:
>
> Intent intent = new Intent(Intent.ACTION_PICK,
> ContactsContract.Contacts.CONTENT_URI);
> intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
> startActivityForResult(intent, REQUEST_CONTACT_NUMBER);
>
> It seems that every contact is only shown once - and if chosen the contact
> picker prompts to select the correct part (phone, email, etc.) - but no
> matter what I select nothings actually gets returned - it always "toasts"
> the text "No number".
>
> I just need a reliable and simple (to the user) way to pick a contact and
> get the phone number into the app - any help or reference for more
> information would be much appreciated ;-)
>
> --
> Michael Banzon
> http://michaelbanzon.com/
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.