How to find all numbers of a single contact in android programmatically? - android

I have an application where I want to fetch multiple numbers of a single contact on the click of the item.
I have a list view of contact in which I want multiple numbers of a contact if the contact has more than 1 number.
Here is my code ,
I get all the numbers but can't get the multiple numbers on Click.
MainActivity.java
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
String phone = null;
String emailContact = null;
String emailType = null;
String image_uri = "";
Bitmap bitmap = null;
if (cur.getCount() > 0) {
while (cur.moveToNext())
{
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
image_uri = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[]{id}, null);
while (pCur.moveToNext())
{
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String phone1 = pCur
.getString(pCur
.getColumnIndex(String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)));
Log.e("PHONE",phone1);
System.out.println("phone" + phone);
}
pCur.close();
Cursor emailCur = cr.query
(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[]{id}, null);
while (emailCur.moveToNext())
{
emailContact = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if(TextUtils.isEmpty(emailContact)||emailContact.equalsIgnoreCase(null)||
emailContact.equalsIgnoreCase("")){
emailContact="";
Log.e("isEmpty","isEmpty " + emailContact);
}else{
Log.e("gfdszfg","Email " + emailContact);
}
/* emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));*/
Log.e("gfdszfg","Email " + emailContact);
}
emailCur.close();
}
if (image_uri != null) {
System.out.println(Uri.parse(image_uri));
try {
bitmap = MediaStore.Images.Media
.getBitmap(this.getContentResolver(),
Uri.parse(image_uri));
System.out.println(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mList.add(new Contacts(name, phone, image_uri,emailContact));
emailContact="";
}
cur.close();
mMyContactsAdapter = new MyContactsAdapter(MainActivity.this,
mList);
mcontact.setAdapter(mMyContactsAdapter);
}
}

It seems like you're properly iterating through all phones for each contact, but when the while loop ends, phone is pointing only to the last phone you've found.
...
List<String> phonesList = new ArrayList<String>();
while (pCur.moveToNext()) {
phone = pCur.getString( ... Phone.NUMBER));
// add the phone to a growing list of phones
phonesList.add(phone);
}
pCur.close();
...
// Change 'Contacts' constructor to accept List<String>, rather then String for phone
mList.add(new Contacts(name, phonesList, image_uri,emailContact));
...
Of course you can do the same for emails, and if you want the label of the phone too, you'll need a list of pairs: List<Pair<String, String>> to hold pairs of phone+label.
On a side note the performance of this code can be improved significantly by querying ContactsContract.Data.CONTENT_URI for ALL emails and phones on the device, putting them all in a HashMap, and then iterating all contacts, and getting the emails and phones from the HashMap.
This will reduce the number of queries from <number of contacts>*2 to just 2.

Related

How to fetch recent and favourite contacts in android?

I have been trying to fetch recent and favorites contacts but every time i get error .
i am storing contacts in database after fetching .
cannot read column -1
and sometimes it says cursor not initialized properly.
please help me .
Here is my code.
ContentResolver cr = getActivity().getContentResolver();
/* Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null );*/
Cursor cur=cr.query(CallLog.Calls.CONTENT_URI,null,CallLog.Calls.DATE, null,null);
String phone = null;
String emailContact = null;
String image_uri;
Bitmap bitmap;
final SQLiteDatabase mDb = db.getWritableDatabase();
mDb.beginTransaction();
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
image_uri = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
System.out.println("name : " + name + ", ID : " + id);
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[]{id}, null);
Log.e("pCur","dfgfdg "+pCur.getCount());
while (pCur.moveToNext())
{
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// contactid=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
/* phonenumber.add(pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));`*/
}
pCur.close();
Cursor emailCur = cr.query
(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[]{id}, null);
while (emailCur.moveToNext())
{
emailContact = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if(TextUtils.isEmpty(emailContact)||emailContact.equalsIgnoreCase(null)||emailContact.equalsIgnoreCase(""))
{
emailContact="";
Log.e("isEmpty","isEmpty " + emailContact);
}
else
{
Log.e("gfdszfg","Email " + emailContact);
}
/* emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));*/
Log.e("gfdszfg","Email " + emailContact);
}
emailCur.close();
}
if (image_uri != null)
{
System.out.println(Uri.parse(image_uri));
try
{
bitmap = MediaStore.Images.Media
.getBitmap(getActivity().getContentResolver(),
Uri.parse(image_uri));
System.out.println(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mList.add(new Contacts(name, phone, image_uri,emailContact));
ContentValues contentValues = new ContentValues();
contentValues.put("contact_name", name);
contentValues.put("contact_number",phone);
contentValues.put("contact_email",emailContact);
contentValues.put("contact_image",image_uri);
mDb.insert(TABLE_CONTACT, null, contentValues);
emailContact="";
phone="";
}
mDb.setTransactionSuccessful();
mDb.endTransaction();
cur.close();
}
You're querying the CallLog table:
Cursor cur=cr.query(CallLog.Calls.CONTENT_URI,null,CallLog.Calls.DATE, null,null)
and then trying to get info from that cursor with fields from the Contacts table:
cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Obviously, that's not how it should work.
Also, your selection:
CallLog.Calls.DATE
is not a legal selection string.
How you should do it:
To get a list of starred (favourites) contacts:
String[] projection = new String[] { Contacts._ID }; // you can add more fields you need here
Cursor cursor = cr.query(Contacts.CONTENT_URI, projection, Contacts.STARRED + "=1", null, null);
To get a list of contacts with in the last 24 hours:
String[] projection = new String[] { Contacts._ID }; // you can add more fields you need here
int oneDay = (1000 * 60 * 60 * 24);
long last24h = (System.currentTimeMillis() - oneDay);
Cursor cursor = cr.query(Contacts.CONTENT_URI, projection, Contacts. LAST_TIME_CONTACTED + ">" + last24h, null, null);
I know till now you may have solved that error but still i want to answer for further help for seekers. The code for favorite contact fetching is working you just need to add following in the projection:
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY};

Get Contacts from Phone only android

In my app i need contacts from phone only not from other accounts like gmail and Facebook.With my code i am getting the contacts but issue is that i am getting it from all the sources like gmail ,facebook etc.
Code
contactResolver = context.getContentResolver();
Cursor cur = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
contactsModel = new ContactsModel();
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
contactsModel.name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
phoneCursor = contactResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (phoneCursor.moveToNext()) {
// Do something with phones
int phoneType = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNo = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//you will get all phone numbers according to it's type as below switch case.
switch (phoneType) {
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
phone = phoneNo;
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
phone = phoneNo;
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
phone = phoneNo;
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
phone = phoneNo;
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
phone = phoneNo;
break;
default:
phone = phoneNo;
break;
}
listPhones.add(phoneNo);
}
/*get email*/
emailCursor = contactResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
while (emailCursor.moveToNext()) {
//String emai = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
int type = emailCursor.getInt(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String s = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), type, "");
listEmails.add(emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
}
contactsModel.emails = listEmails;
contactsModel.numbers = listPhones;
listContacts.add(contactsModel);
listEmails = new ArrayList<>();
listPhones = new ArrayList<>();
emailCursor.close();
phoneCursor.close();
}
}
cur.close();
return listContacts;
So how will i get the contacts from only phonebook.
Try below query:
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1",
null,
null
);
where, IN_VISIBLE_GROUP: 1=local, 0=google account
Hope it will help you
public void readContacts() {
StringBuffer sb = new StringBuffer();
sb.append("......Contact Details.....");
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
String phone = null;
String emailContact = null;
String emailType = null;
String image_uri = "";
Bitmap bitmap = null;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
image_uri = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
sb.append("\n Contact Name:" + name);
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("\n Phone number:" + phone);
System.out.println("phone" + phone);
}
pCur.close();
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (emailCur.moveToNext()) {
emailContact = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
sb.append("\nEmail:" + emailContact + "Email type:" + emailType);
System.out.println("Email " + emailContact
+ " Email Type : " + emailType);
}
emailCur.close();
}
if (image_uri != null) {
//System.out.println(Uri.parse(image_uri));
try {
bitmap = MediaStore.Images.Media
.getBitmap(this.getContentResolver(),
Uri.parse(image_uri));
sb.append("\n Image in Bitmap:" + bitmap);
// System.out.println(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sb.append("\n........................................");
}
textDetail.setText(sb);
}
}

JSONArray or JSONObject of value to bundle and send to server in android

i'm new to android and probably this is silly question but please help. i am receiving output as
contact Name: RRRR
phone Number: XXXXXXXXX
contact Name: SSSS
phone Number: YYYYYYYYY
phone Number: aaaaaaaaa
phone Number: zzzzzzzzz
contact Name: TTTT
phone Number: XXXXXXXXX
phone Number: ccccccccc
.
.
.
//code
public void readContacts() {
StringBuffer sb = new StringBuffer();
sb.append("......Contact Details.....");
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String phone = null;
String name = null;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur .getColumnIndex(ContactsContract.Contacts._ID));
name = cur .getString(cur .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer .parseInt(cur.getString(cur .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id); sb.append("\n Contact Name:" + name);
Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
phone = pCur .getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("\n Phone number:" + phone+":"+pCur.getCount()); System.out.println("phone" + phone);
}pCur.close();
}
outputText.setText(sb);
}
}
}
P.S: how can i store the ouput in container and then bundle it and send to server ?
thanks in advance,
I tried to put list of contacts in JSONArray,
Step-1 : Declare JSONArray finalJarray; for in future use to send JSONArray to server.
Step-2 : Generate JSONArray with contact name and multiple phone number. Unique key name to multiple phone numbers so that it can not override.
public void readContacts() {
try {
finalJarray = new JSONArray();
StringBuffer sb = new StringBuffer();
sb.append("......Contact Details.....");
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
String phone = null;
String name = null;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
JSONObject contactObject = new JSONObject();
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
sb.append("\n Contact Name:" + name);
contactObject.put("contact_name", name);
Cursor pCur = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id },
null);
int tmpPhoneCount = 0;
while (pCur.moveToNext()) {
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("\n Phone number:" + phone + ":"
+ pCur.getCount());
System.out.println("phone" + phone);
contactObject.put("phone_number_"+tmpPhoneCount, phone);
tmpPhoneCount++;
}
pCur.close();
finalJarray.put(contactObject);
}
// outputText.setText(sb);
}
}
System.out.println("finalJarray.toString() = "
+ finalJarray.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
I tried on my device, let me know if any query.

Android save LocalContacts into Database

Here is how local contacts are saved into database 1 by 1. This method lasts for about 20-30 sec. The while works really slow
public StringBuilder getLocalContacts(Context context, List<Contact> contactList) {
StringBuilder sb = new StringBuilder();
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
// this while works slow
while (cursor.moveToNext()) {
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.valueOf(hasPhone) == 1) {
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
// You know it has a number so now query it like this
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
Contact contact = new Contact();
contact.setFullName(contactName);
contact.setAddressBookId(contactId);
String phoneNumber = phones.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact.setPhone(phoneNumber);
sb.append(phoneNumber).append("|");
Cursor emails = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
String emailAddress = emails.getString(emails
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
contact.setEmail(emailAddress);
break;
}
emails.close();
contactList.add(contact);
}
phones.close();
}
}
cursor.close();
return sb;
}
Does anyone have any idea what would make this method do the job in about 1 sec? or is there any faster method?

How to get contact email id?

I have a listview of all the contact names in the phone. I want to get the email id (if contact have one) of the contact which I click on in the listview. How can I do this?
Use the following code to get all email ids. I checked the code. It is working.
public static void getContactEmails(Context context) {
String emailIdOfContact = null;
int emailType = Email.TYPE_WORK;
String contactName = null;
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(BaseColumns._ID));
contactName = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// Log.i(TAG,"....contact name....." +
// contactName);
cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
Cursor emails = cr.query(Email.CONTENT_URI, null,
Email.CONTACT_ID + " = " + id, null, null);
while (emails.moveToNext()) {
emailIdOfContact = emails.getString(emails
.getColumnIndex(Email.DATA));
// Log.i(TAG,"...COntact Name ...."
// + contactName + "...contact Number..."
// + emailIdOfContact);
emailType = emails.getInt(emails
.getColumnIndex(Phone.TYPE));
}
emails.close();
}
}// end of contact name cursor
cur.close();
}
Phone Numbers
Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
// Do something with phones
}
pCur.close();
}
Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.
Email Addresses
Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,Uri.encode(name.toString().trim()));
Cursor mapContact = getContext().getContentResolver().query(uri, new String[]{PhoneLookup._ID}, null, null, null);
if(mapContact.moveToNext())
{
String _id = mapContact.getString(mapContact.getColumnIndex(ContactsContract.Contacts._ID));
}
Xamarin Version of Sunil's answer. Took me a while, but I figured it out.
ContentResolver cr = ContentResolver;
string contactName = null;
var cur = cr.Query(ContactsContract.Contacts.ContentUri,null,null,null,null);
if (cur.MoveToFirst())
{
do
{
string id = cur.GetString(cur.GetColumnIndex(BaseColumns.Id));
contactName = cur.GetString(cur.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.DisplayName));
var emails = cr.Query(ContactsContract.CommonDataKinds.Email.ContentUri, null, ContactsContract.CommonDataKinds.Email.InterfaceConsts.ContactId + " = " + id, null, null);
if (emails.MoveToFirst()) {
do
{
// This is where it loops through if there are multiple Email addresses
var email = emails.GetString(emails.GetColumnIndex(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Data));
} while (emails.MoveToNext());
}
} while (cur.MoveToNext());
}
I am using below code. it is working fine. checked it.
ArrayList<ContactInfo> listContactsData = new ArrayList<>();
// Retrieve Email address
Cursor emailCursor = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCursor.moveToNext()) {
// This would allow you get email addresses
String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); String emailType = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Log.e(“Email :“,” ”+email)
objContact.strEmail = email;
}
emailCur.close();
listContactsData.add(objContact);
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String hasNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String email= "";
if (Integer.valueOf(hasNumber) == 1) {
Cursor numbers = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (numbers.moveToNext()) {
email= numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
//Toast.makeText(getApplicationContext(), "Number=" + num, Toast.LENGTH_LONG).show();
//asdasdasdsa
if(getEmail(email).isEmpty()){
Toast.makeText(this, "Email Not Found In That Contact Try Another", Toast.LENGTH_SHORT).show();
}
else {
edt_email_contact.setText("" + getEmail(email));
} }
}
}
break;
}

Categories

Resources