Get email address from phone number in Android - android

I'm attempting to find someone's email address, given their phone number (assuming they have it stored in Contacts).
At the moment I've been fine if I want the person's name, or something, just doing:
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cur = getContentResolver().query(uri, null, null, null, null);
String name = cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
However, there is no way to search using a phone number, through the email DB
Uri emailUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI,
Uri.encode(phoneNumber));
Cursor cur = getContentResolver().query(emailUri, null, null, null, null);
//Returns nothing, unless someone has the email +441234567891#hotmail.com or something
Now I could use CONTENT_FILTER_URI, and search for the name of the person I just found... But then I could get 2 John Smiths... There must be some way to just join across the PhoneLookup and the Email DBs? Maybe using the LOOKUP_KEY, or something?

Related

How to search contacts by name and phone number?

I am using Contacts.CONTENT_FILTER_URI to search for contacts.
Uri contentUri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_FILTER_URI,
Uri.encode(searchString));
The searchstring can be either a number or a name. That works great.
My only problem is that the result does not contain a contact phone number.
I know that I can get it by querying ContactsContract.Data.CONTENT_URI. However, I would like to find a solution that will give me a contact name and phone number with a single query.
You should use Phone.CONTENT_FILTER_URI instead of Contacts.CONTENT_FILTER_URI
Docs say:
The filter is applied to display names as well as phone numbers.
Try this:
Uri filterUri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(searchString));
String[] projection = new String[]{ Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor cur = getContentResolver().query(filterUri, projection, null, null, null);

How can I access ContactsContract Profile?

I tried to read the complete profile information such a (Full name, phone, adresse, mail .... ).
I have searched everywhere for a good example code. I tried many ways (Uri => Cursor) to access the Profile.
At this time I can fetch just the Full name (of the Profile contact), nothing more.
I can fetch data of other contacts using an Intent, sending to the Contacts app, BUT I CAN'T READ THE PROFILE CONTACT (JUST FULL NAME).
I have added the READ_PROFILE permission in the manifest file.
With the following code I get the Full Name (I can also access first and last name separately ):
Uri uriProfile = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
Cursor cursorProfile = this.getContentResolver().query(uriProfile,
null, null, null, null);
String projection = Profile.DISPLAY_NAME;
String profileName = cursorProfile.getString(cursorProfile.getColumnIndex(projection);
But when I use this the following projection to get Phone Number, it returns an error and the app stops working:
String projection = ContactsContract.CommonDataKinds.Phone.NUMBER
I found a solution using this code:
Get an URI Profile
Crate a cursor pointing to the URI content with null projection. Because for the profile, data are saved differently than a normal contact.
Point the cursor to the wanted data using MIMETYPE.
Uri uriProfile = Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
Cursor cursorProfile = getApplicationContext().getContentResolver().query(uriProfile,
null, null, null, null);
String cursorProfile_MIMIETYPE = cursorProfile.getString(cursorProfile.getColumnIndex("MIMETYPE"));

Get all contacts and their details (e.g. postal address) in one OUTER JOIN query

I know how to retrieve contact data for specific contacts. However, i can't find a way to get all contacts plus some of their details in a single query. The following code gets all contacts having a postal address:
Uri uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
String[] projection = new String[] {
StructuredPostal._ID,
StructuredPostal.LOOKUP_KEY,
StructuredPostal.DISPLAY_NAME,
StructuredPostal.STREET,
StructuredPostal.CITY
};
String sortOrder = StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor c = getContentResolver().query(uri, projection, null, null, sortOrder);
But what i need are all contacts, whether they have postal address or not. Is this doable using the ContatsContract API, or do i need to create custom outer join query? Any hints on how?
if You have a contact ID and you want to fetch the Postal Address then use this :
Uri postal_uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
Cursor postal_cursor = getContentResolver().query(postal_uri,null, ContactsContract.Data.CONTACT_ID + "="+contactId.toString(), null,null);
while(postal_cursor.moveToNext())
{
String Strt = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.STREET));
String Cty = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.CITY));
String cntry = postal_cursor.getString(postal_cursor.getColumnIndex(StructuredPostal.COUNTRY));
}
postal_cursor.close();
All contact information in Android 2.0 is stored in a single database table. So you can get all the information you need in a single query:
Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
null, null, null, sortOrder);
The just iterate through the data and check Data.MIMETYPE column. For example, if this column has StructuredPostal.CONTENT_ITEM_TYPE value, then you can get StructuredPostal fields from this column.

Retrieving the Phone number from a contact using "ContactsContract" by an ID?

I'm able to retrieve the display name of contact using the id Code as follows:
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, selectedid);
Cursor cur = managedQuery(uri, null, null, null, null);
startManagingCursor(cur);
cur.moveToNext();
String mname = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Where the "selectedid" is my id of the contact to be retrieved.
Now i have a problem in retrieving the phone number from that id.
If possible can u type me the code which i have to add to get the phone number from that id
Have you tried:
String number = cur.getString(cur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
i kind of figured it out .... try the link bellow...
Retrieve Contact Phone Number From URI in Android

contacts in android

I want to get all phone contacts from device in android.i have used the following code.but the problem is it takes more time to return the results.is there any solution?
ContentResolver cr = getContentResolver();
int index=0;
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0)
{
phoneNames=new String[cur.getCount()];
phoneNumbers=new String[cur.getCount()];
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)
{
phoneNames[index]=name;
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext())
{
phoneIndex++;
phoneNumbers[index] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
index++;
}
pCur.close();
}
}
After reading the code i assume that what you want is a list of contacts with DISPLAY NAMES and their respective phone numbers.
If you are specifically looking for data related to phone numbers i suggest you query on
android.provider.ContactsContract.PhoneLookup and fetch the results using a single cursor.
The following are the fields that you would be interested in:
DISPLAY_NAME
HAS_PHONE_NUMBER
NUMBER
TYPE
e.g
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME,...
Further details please refer this
Please post your requirement if the assumptions are not true.
Some of quick checks:
Select only the required columns and not all in the first query.
Instead of using Integer.parseInt(cur.getString) use
cur.getInt()
Use PhoneLookup whenever dealing with phone numbers ( the number
field gives the raw phone number
instead of the value stored in
the database which can contain
-,),( appended with it)
Avoid using Cursor within a cursor. Use the API's which includes
joins already implemented in it like
RawContactsEntity, PhoneLookup.
Hope that helps.
Don't do complex database queries on the UI thread. What are you trying to do with the results? If you are displaying things in a list, use a CursorAdapter so that you only pull out what you need when you need it.

Categories

Resources