Dear Mohd,

Thank you for your reply.

It seems that the code you provided query to get the name and phone number
for all contacts. This is not what I want - I want to user to pick a
contact so I can retrieve the number of the contact.

As a side note: Please don't use catch with the Exception master class. And
e.printStackTrace() won't do much on (standard) Android ;-)


On Sun, Nov 17, 2013 at 10:11 PM, Mohd Arshi Khan <arshikha...@gmail.com>wrote:

> try this
> ---------
> Cursor phones;
> try {
>             phones = getContentResolver().query(
>                     ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
> null, null,
>                     null,
> ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC ");
>
> while (phones.moveToNext()) {
>
>                     String name = phones
>                             .getString(phones
>
> .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
>
>                     String phoneNumber = phones
>                             .getString(phones
>
> .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
>
>                     objContact = new ContactFields();
>                     objContact.setName(name);
>                     objContact.setPhoneNumber(phoneNumber);
>                     list.add(objContact);
>
>
>
>             }
>
>         } catch (Exception e) {
>             // TODO Auto-generated catch block
>             e.printStackTrace();
>         }finally{
>                 phones.close();
>         }
>
>
>
> On Sun, Nov 17, 2013 at 6:36 PM, Michael Banzon <mich...@banzon.dk> 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 android-developers@googlegroups.com
>> To unsubscribe from this group, send email to
>> android-developers+unsubscr...@googlegroups.com
>> 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 android-developers+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> 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 android-developers+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>



-- 
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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
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 android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to