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_CONTACT
in this example). This will cause Android to launch an Activity that's registered to support ACTION_PICK
on 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 void onActivityResult(int reqCode, int resultCode, Intent data) {
super.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
Fetching Contacts from Devise
ReplyDeleteGreat stuff! Thanks!
ReplyDeleteHi,
ReplyDeleteIs there a flag to set which will allow multiple contacts selection?
Thanks
great...
ReplyDeletehow can I insert a name from recognizer results to get a phone number?
ReplyDeleteis dere any way to pic contacts from a call log..
ReplyDeletei want to select multiple contacts using checkbox and return with contact name and phone number on a button click-OK.
ReplyDeletePlease Help...thanks in advance
Can we call from this app??
ReplyDeletehow 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
ReplyDeletehow 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?
ReplyDeleteHere me took One variable
Deletedeclare final int PICK_CONTACT=1;
thats enough , it's like identification variable.
popular android app in India check this https://play.google.com/store/apps/details?id=com.smartappgenerator.dailyquiz
ReplyDeleteThank you very much!!! =)
ReplyDeletehow to access multiple contact nos
ReplyDeleteThank you very much.
ReplyDeleteThose 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);
}
can anybody tell me that this code will able to show contacts both from phone memory and sim memory????????
ReplyDeletepublic ArrayList getNameEmailDetails() {
ReplyDeletename = 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;
}
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
ReplyDeletefor this Read About Content provider , using this class you can get information about Contacts , still if you face any problems ask me .
DeleteThis code is ok but when i click on contack for call my application is stop.. ??
ReplyDelete