Nov 10, 2011

How to call Android contacts list?

Hi this is very import concept to Read Existing Contacts from Your Emulator or Device

There are three steps to this process.

1) Permissions

Add a permission to read contacts data to your application manifest.

 android:name="android.permission.READ_CONTACTS"/>

2) Calling the Contact Picker

Within your Activity, create an Intent that asks the system to find an Activity that can perform a PICK action from the items in the Contacts URI.

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

Call startActivityForResult, passing in this Intent (and a request code integer, PICK_CONTACTin this example). This will cause Android to launch an Activity that's registered to support ACTION_PICKon the People.CONTENT_URI, then return to this Activity when the selection is made (or canceled).

startActivityForResult(intent, PICK_CONTACT);

3) Listening for the Result

Also in your Activity, override the onActivityResult method to listen for the return from the 'select a contact' Activity you launched in step 2. You should check that the returned request code matches the value you're expecting, and that the result code is RESULT_OK.

You can get the URI of the selected contact by calling getData() on the data Intent parameter. To get the name of the selected contact you need to use that URI to create a new query and extract the name from the returned cursor.

@Override
public vo
id onActivityResult(int reqCode, int resultCode, Intent data) {
sup
er.onA


ctivityResult(reqCode, resultCode, data);

switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}

Home Screen If No Contacts Contacts










For Online Training :

For Full Source Cliked Here

20 comments:

  1. Fetching Contacts from Devise

    ReplyDelete
  2. Great stuff! Thanks!

    ReplyDelete
  3. Hi,
    Is there a flag to set which will allow multiple contacts selection?
    Thanks

    ReplyDelete
  4. how can I insert a name from recognizer results to get a phone number?

    ReplyDelete
  5. is dere any way to pic contacts from a call log..

    ReplyDelete
  6. i want to select multiple contacts using checkbox and return with contact name and phone number on a button click-OK.
    Please Help...thanks in advance

    ReplyDelete
  7. Can we call from this app??

    ReplyDelete
  8. how to read the phone number from mobile/sim contacts and also i need to assign the pnone number into one textbox....can you help me....Email:mageshmca89@gmail.com

    ReplyDelete
  9. how do I use the PICK_CONTACT value? is there something to import first? startActivityForResult(intent, PICK_CONTACT); line says there is no such value "PICK_CONTACT". What am I missing?

    ReplyDelete
    Replies
    1. Here me took One variable

      declare final int PICK_CONTACT=1;
      thats enough , it's like identification variable.

      Delete
  10. popular android app in India check this https://play.google.com/store/apps/details?id=com.smartappgenerator.dailyquiz

    ReplyDelete
  11. Thank you very much!!! =)

    ReplyDelete
  12. how to access multiple contact nos

    ReplyDelete
  13. Thank you very much.
    Those who are looking for the "CONTACT NUMBER"

    Here is the code. This code in if condition

    Uri uri = data.getData();
    Cursor cursor=managedQuery(uri, null, null, null, null);
    String phoneNumber = null;
    while (cursor.moveToNext()) {
    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    Log.d("HELLO : ", " "+contactId);
    String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
    Log.d("HELLO : ", " "+hasPhone);
    if ((Integer.parseInt(hasPhone)==1 )){
    Log.d("HELLO : ", " IN if");
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
    null,
    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
    null, null);
    while (phones.moveToNext()) {
    phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }
    phones.close();
    }
    Log.d("HELLO : ", " "+name);
    Log.d("HELLO : ", " "+phoneNumber);
    }

    ReplyDelete
  14. can anybody tell me that this code will able to show contacts both from phone memory and sim memory????????

    ReplyDelete
  15. public ArrayList getNameEmailDetails() {
    name = new ArrayList();
    email_array = new ArrayList();
    ArrayList temp = new ArrayList();
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
    null, null, null);
    if (cur.getCount() > 0) {
    this.name.clear();
    while (cur.moveToNext()) {
    String id_email = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
    long id1 = cur.getLong(cur.getColumnIndex(ContactsContract.Contacts._ID));
    Cursor cur1 = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
    + " = ?", new String[] { id_email }, null);
    while (cur1.moveToNext()) {
    // to get the contact names
    obj_invitation = new Invitation();
    obj_invitation.setName(cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
    obj_invitation.name = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    obj_invitation.setEmail(cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
    obj_invitation.email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

    String email_of_phones = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id1);

    // ******* getting photos of contact

    Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,String.valueOf(id_email));
    InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),my_contact_Uri);
    BufferedInputStream BufferedInputStream = new BufferedInputStream(photo_stream);
    Bitmap my_btmp = BitmapFactory.decodeStream(BufferedInputStream);

    // **************** for contact email validation


    // if(Utils.isEmailValid(email_of_phones) == true){
    // obj_invitation.setProfile_pic(my_btmp);
    // this.name.add(obj_invitation);
    // list.add(obj_invitation.email);
    // email_array.add(email_of_phones);
    // }
    // else{
    // Log.e("", "Email not found ::::::::::");
    // }

    obj_invitation.setProfile_pic(my_btmp);
    this.name.add(obj_invitation);
    list.add(obj_invitation.email);
    email_array.add(email_of_phones);
    }
    }
    }

    // ********************** converting String to json Array and vice-verse
    JSONArray jsArray = new JSONArray(list);
    contact_email_array = jsArray.toString();
    invitefriends = new invitefriendsAdpter(getContext(), this.name,name_status, invite,null);
    incontact_list.setAdapter(invitefriends);
    invitefriends.notifyDataSetChanged();
    Log.e("email_array", "" + email_array);
    return this.name;
    }

    ReplyDelete
  16. I want to select multiple contacts using checkbox and return with contact name and phone number on a button click-OK. can any help me

    ReplyDelete
    Replies
    1. for this Read About Content provider , using this class you can get information about Contacts , still if you face any problems ask me .

      Delete
  17. This code is ok but when i click on contack for call my application is stop.. ??

    ReplyDelete