In Android, how do you select only PHONE contacts? - android

I am querying the Contacts out of the built in Contacts provider URI in Android. I want to get just the PHONE contacts; is there any consistent way to do this? It seems from what I can find that the account name for phone contacts differs from manufacturer to manufacturer (see this question). Is there a way to get PHONE contacts (not SIM, Facebook, Twitter or others) in a consistent, reliable, manufacturer- and device- agnostic way?

Cursor cursor = null;
try {
String selection = ContactsContract.Data._ID + " = ?";
String[] selectionArgs = new String[] { id };
String[] projection = new String[] { ContactsContract.PhoneLookup.NUMBER};
cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
projection, selection, selectionArgs, null);
if (cursor == null || !cursor.moveToFirst())
return;
String phone = cursor.getString(0);
} finally {
if (cursor != null && !cursor.isClosed())
try {
cursor.close();
} catch (Throwable ignore) {
// Ignored.
}
}
Where "?" is user ID, you can put this code into a loop.

Related

All Contacts are not showing while retrieving contacts

I implemented a code to retrieve all contacts but it is not showing all contacts where few of them are missed.
Here is my code:
String[] projection = new String[]{
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER,
};
Cursor cursor = null;
try {
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, null, null, null);
} catch (SecurityException e) {
}
if (cursor != null) {
try {
HashSet<String> normalizedNumbersAlreadyFound = new HashSet<>();
int indexOfNormalizedNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER);
int indexOfDisplayName = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexOfDisplayNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
while (cursor.moveToNext()) {
String normalizedNumber = cursor.getString(indexOfNormalizedNumber);
if (normalizedNumbersAlreadyFound.add(normalizedNumber)) {
String displayName = cursor.getString(indexOfDisplayName);
String displayNumber = cursor.getString(indexOfDisplayNumber);
listOfContacts.add(new PhoneContactsModel(displayName, displayNumber, false));
} else {
}
}
Log.d("tag", "size of listOfContacts =1====" + listOfContacts.size());
} finally {
cursor.close();
}
}
don't know what is happening. Please help me.
There are many issues in the code:
You're querying over the CommonDataKinds.Phone.CONTENT_URI table, so naturally, you won't get contacts that have no phone numbers (e.g. contacts with name and email)
You're skipping contacts that contains phones you've already encountered in normalizedNumbersAlreadyFound, so if you have two contacts with a shared phone (like a home phone number) you might skip one of them.
CommonDataKinds.Phone.NORMALIZED_NUMBER may be null, in which case you'll skip many contacts that do not have their NORMALIZED_NUMBER field set
If you need to also include contacts that have no phones, I would recommend a completely different code. If you only need to get contacts with phones, I would recommend not relying on NORMALIZED_NUMBER, and instead add CommonDataKinds.Phone.CONTACT_ID to your projection, and have that as your unique key per contact.

How to obtain the contact Uri directly with contact creation

my app creates new contacts with ContentProviderOperation. The problem is, I need a reference to the new contact because I need some information of it to display it in a listview and go with intent into the contact app to the contact.
The best thing would be the ID, but I´ve read that it might change during operations on the database, which won´t be helpful to me.
Now I thought, the Uri might be the best thing, because I could later retrieve the contactID or lookup key.
How do I get the Uri directly after calling applyBatch() ?
EDIT:
Here is a solution, but not really a good one.
He is putting a randomly generated token into each contact, then he makes a new query with it.
I want neither put some extra data into the contacts, nor starting a second query. But if there is no other possibility I´ll do it that way.
simply call
private String retrieveContactId(String phoneNo) {
try {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNo));
String[] projection = new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME };
String selection = null;
String[] selectionArgs = null;
String sortOrder = ContactsContract.PhoneLookup.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
ContentResolver cr = getApplicationContext().getApplicationContext().getContentResolver();
String contactId = null;
if (cr != null) {
Cursor resultCur = cr.query(uri, projection, selection, selectionArgs, sortOrder);
if (resultCur != null) {
while (resultCur.moveToNext()) {
contactId = resultCur.getString(resultCur.getColumnIndex(ContactsContract.PhoneLookup._ID));
Log.e("Info Incoming", "Contact Id : " + contactId);
return contactId;
}
resultCur.close();
}
}
} catch (Exception sfg) {
Log.e("Error", "Error in loadContactRecord : " + sfg.toString());
}
return null;
}
and for the uri
Uri contactUri = Contacts.getLookupUri(
Integer.valueOf(rawContactId), clookup);

how to get the group id of a contact in android

I want to get the group id of each contact in my android application.I have initial query like this
Cursor cursor = cr.query(Phone.CONTENT_URI,
new String[] {
Phone.CONTACT_ID}, null, null, null);
And what i do is get details of each which related to this CONTACT_ID.I have manage to get phone number,names,addresses and emails so far but still couldn't manage to get the group id of the contact.
P.S i found a question asked before here but the class android.provider.Contacts.GroupMembership is deprecated.
thanks.
Try this:
final String selection = "mimetype_id = (select _id from mimetypes where mimetype = \"" +
vnd.android.cursor.item / group_membership + "\")";
Cursor cursor = getContentResolver().query(Data.CONTENT_URI,
new String[]{Phone.CONTACT_ID}, selection, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
do {
Log.i("Details", "Contact IDs" + cursor.getLong(cursor.getColumnIndex(Phone.CONTACT_ID)));
} while (cursor.moveToNext());
}
} finally {
if (cursor != null) {
cursor.close();
}
}

Display contact names by part of phone number

I have a function that takes a number and seeks for it into the phone contacts, but after that it returns the contact name.
public String[] getContactDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String name = "?";
String[] nameStr = {};
ContentResolver contentResolver = getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
try {
while (contactLookup != null && contactLookup.getCount() != 0 && !contactLookup.isLast()) {
contactLookup.moveToNext();
nameStr = insertElement(nameStr,contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
//String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return nameStr;
}
What do I got to modify to make it search for a part of a number?
ex if the number is:0729587347, I want to search for 0729.
If there are many contact starting with that number, I want them all(It is something like MYSQL %variable% search ...)!
What's to do ?
Thanks !
The ContentResolver.query(...) can take a WHERE clause parameter.
Specifically, use something like:
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Phone.NUMBER + " like ?",
new String[] {"%"+String.valueOf(userInput) +"%"}, null);
For more examples, see the ContactsContract.Data javadoc.
Also, this is similar to a previous question.

Get Contacts mobile number only

There have been several questions on SO regarding getting Contacts numbers using the Contacts API but I'd like to know if there is a way to identify that the number retrieved is a mobile number.
The following code is often shown as a way to get a Contact's phone numbers, as it gets a list of one or more phone numbers:
String[] projection = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER};
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";
Cursor cursor = null;
Cursor phones = null;
try
{
cursor = managedQuery(intent.getData(), projection, selection, null, null);
while (cursor.moveToNext())
{
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
String pdata = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
Log.v("DATA",pdata);
}
}
}
catch (NullPointerException npe)
{
Log.e(TAG, "Error trying to get Contacts.");
}
finally
{
if (phones != null)
{
phones.close();
}
if (cursor != null)
{
cursor.close();
}
}
Whilst, this works okay, is there any way to easily identify that the phone number is a mobile type (apart from trying to pattern match with Regex).
I suspect there must be a related piece of data, so that native apps can classify the phone number - as in the picture below:
I stumbled upon a blog article which gives a pretty good explanation of using the ContactsContract api here.
So, in my example above, I changed part of my code above to this:
while (phones.moveToNext())
{
int phoneType = phones.getInt(phones.getColumnIndex(Phone.TYPE));
if (phoneType == Phone.TYPE_MOBILE)
{
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
phoneNumber = phoneNumber.replaceAll("\\s", "");
break;
}
}
This loops around all the phones for an individual contact and if the type is Phone.TYPE_MOBILE then it takes this one.
Hope this helps someone with the same issue I had.
The original query cursor = managedQuery(intent.getData(), projection, selection, null, null); should be able to handle filter for you. I am trying to do the same thing. I will post my findings when complete. If anyone knows how to do this using the selection clause in the third parameter. I would love to know the answer.

Categories

Resources