I'm attempting to retrieve both email and phone via a single cursor described below. I'm using the Email.CONTENT_URI, hence I don't retrieve the phone number, so my phoneColumn is returning email. I tried using Phone.CONTENT_URI but it only returns a smaller subset of contacts (possibly because it only fetches ones that have phone numbers). Is there a way to get both email and phone with a specific Uri or how can I do it with two cursors?
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
projection, null, null, order);
int idColumn = cursor.getColumnIndex(ContactsContract.Data._ID);
int nameColumn = cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
int emailColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
int phoneColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA);
where projection is:
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Phone.DATA, }
Please have a look here. You should find all the necessary examples for such kind of querying.
For an example, you can fetch all information of a contactId in this way:
Cursor data = context.getContentResolver().query(
Data.CONTENT_URI, new String[] { Data._ID,Data.MIMETYPE,
Email.ADDRESS, Photo.PHOTO},Data.CONTACT_ID
+ "=?" + " AND " + "(" + Data.MIMETYPE + "='"
+ Photo.CONTENT_ITEM_TYPE + "' OR " + Data.MIMETYPE
+ "='" + Email.CONTENT_ITEM_TYPE +"')",
new String[] {String.valueOf(contactId)}, null);
You can fetch info for all contacts in this way and then probably sort it according to your criteria. You can build queries like this based on your need.
I have created this library to solve all your queries. It will only save contacts with at least one email or phone number. Also it will remove duplicates from emails and phone numbers from same contacts (created by 3rd party apps like whatsapp).
Please have a look at it.
Link : https://github.com/raghavsatyadev/ContactFetcher/
Related
I need a Cursor selecting contacts belonging to specific groups ordered by their family names (not display_names).
It is easy to get a cursor returning the contacts belonging to the requested groups, and another one returning the contacts sorted by family names.
However the family name belongs to DATA records with ContactsContract.Data.MIMETYPE = ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE while contacts belonging to specific groups are to be found in records with ContactsContract.Data.MIMETYPE = ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE.
How can I join records with different CONTENT_ITEM_TYPE but that share a common field, namely RAW_CONTACT_ID?
You said you need to select contacts, if so you shouldn't use RAW_CONTACT_ID, but instead CONTACT_ID to join your contacts data.
A single Contact may be an aggregate of multiple RawContacts and in that case I assume you want all the details of that single contacts as one row.
Now to get what you want, you can't use a Cursor to iterate through the contacts, instead you should load all the data you need to memory (e.g. HashMap) and run through that.
BTW, if you prefer to query over the Contacts/RawContacts tables instead of the Data table, you can utilise DISPLAY_NAME_ALTERNATIVE column to get your sort, see: https://developer.android.com/reference/android/provider/ContactsContract.ContactNameColumns.html#DISPLAY_NAME_ALTERNATIVE
Example code:
int selectedGroupId = 12345;
HashSet<Long> ids = new HashSet<>();
// get all CONTACT_IDs belonging to some GROUP_ID
String[] projection = new String[]{Data.CONTACT_ID};
String selection = Data.MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE + "' AND " + GroupMembership.GROUP_ROW_ID + "=" + selectedGroupId;
Cursor c = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null);
while (c.moveToNext()) {
ids.add(c.getLong(0));
}
c.close();
String[] projection = new String[]{Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1};
// you can add more MIMETYPES to the selection here to get phones, emails, etc. for each contact
String selection = Data.CONTACT_ID + " IN (" + TextUtils.join(",", ids) + ") AND " + Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'";
c = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, StructuredName.FAMILY_NAME + " ASC");
DatabaseUtils.dumpCursor(c);
c.close();
I'm having a problem retrieving all phone contacts.
when using the following selection clause, I'm not getting contacts that not been saved in association to group (such google specific account group):
final static String SELECTION = (Utils.hasHoneycomb()
? Contacts.DISPLAY_NAME_PRIMARY
: Contacts.DISPLAY_NAME) + "<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1";
...
...
// onCreateLoader() code:
return new CursorLoader(getActivity(), Contacts.CONTENT_URI, ContactsQuery.PROJECTION,
ContactsQuery.SELECTION, null, ContactsQuery.SORT_ORDER);
got this snippet from one of the Google tutorials or demo projects.
following the documentation, I realized that Contacts.IN_VISIBLE_GROUP + "=1" causing the filtering of contacts that not belong to any group, so I simply removed this condition expecting to get also the contacts that don't belongs to any group:
final static String SELECTION = (Utils.hasHoneycomb()
? Contacts.DISPLAY_NAME_PRIMARY
: Contacts.DISPLAY_NAME) + "<>''";
after using this SELECTION clause - I got all the contacts includes the ones that not belongs to any group, but now I got duplication of all the contacts that does belongs to a group.
please help me understand how to perform query that returns all contacts without duplication
Thanks in advance
Uri contactUri = data.getData();
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.Contacts.DISPLAY_NAME};
Cursor cursor = activity.getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(column);
int column1 = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String name = cursor.getString(column1);
I have a string content//com.android.contacts/contacts/contacts/2 ie aft the user has selected a particular number.
From this string, how do i get the phone number of this particular contact?
I am new to android environment, so my question might seem a bit primitive.
Cursor c = (Cursor)mAdapter.getItem(a.keyAt(i));
Long id = c.getLong(c.getColumnIndex(ContactsContract.Contacts._ID));
contacts.add(ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id));
chosenContacts = sb.toString();
chosenContacts is my string and that contains content//com.android.contacts/contacts/contacts/2
You need a second request. You can use the id of your snippet for it:
Cursor phoneNoCursor = contentResolver.query(
Data.CONTENT_URI,
new String[] {Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Data.MIMETYPE + " = ? AND " + Data.CONTACT_ID + " = ? ",
new String[] {String.valueOf(Phone.CONTENT_ITEM_TYPE, id)},
null);
For the list of possible types see the description of the Phone class. But maybe the number itself and its label are enough anyway.
The short version of my question is: How do I access the phone numbers of contacts that were synced from 3rd party apps?
Here is the long version:
I can access the regular Android contacts pretty easily. The issue is when the only information in the contact list is synced with a 3rd party app like Facebook or LinkedIn. If I physically went and typed someone's phone number into the Google Contacts List, everything works fine.
However, if this phone number came from syncing my facebook account to my contact list, no phone number shows up, even though if I navigated to Google's pre-made contact list I can see a phone number is actually attached to the contact. Here is the code I use for getting the phone numbers.
public void populateNumberLists(View view)
{
LinearLayout ll = (LinearLayout) view;
TextView tv = (TextView) ll.findViewById(R.id.contactEntryText);
String str = (String) tv.getText();
Cursor cursor = getNumbers(str);
String[] fields = new String[] {
cursor.getColumnName(1).toString()
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.numberentry, cursor,
fields, new int[] {R.id.numberEntryText});
mNumberList.setAdapter(adapter);
}
private Cursor getNumbers(String str)
{
final Uri URIs = ContactsContract.Contacts.CONTENT_URI;
final String ID = ContactsContract.Contacts.LOOKUP_KEY;
String id = "";
ContentResolver cr = getContentResolver();
Cursor cu = cr.query(URIs, null, ContactsContract.Contacts.DISPLAY_NAME + " = '" + str + "'", null, null);
if (cu.moveToFirst()) {
id = cu.getString(cu.getColumnIndex(ID));
}
cu.close();
// Run query
Uri uri = Phone.CONTENT_URI;
String[] projection = new String[] {
Phone._ID,
Phone.NUMBER
};
String selection = Phone.LOOKUP_KEY + " = '" + id + "' and (" + Phone.TYPE + " = '" + Phone.TYPE_HOME+"' or " + Phone.TYPE + " = '" + Phone.TYPE_MOBILE+"' or " + Phone.TYPE + " = '" + Phone.TYPE_WORK+"' or " + Phone.TYPE + " = '" + Phone.TYPE_WORK_MOBILE+"')";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, null);
}
Basically, the populateNumberLists function takes a clicked item from a list view, determines which contact from the list was clicked and calls the function getNumbers.
The getNumbers function takes actual name that was clicked, gets the lookup key for that name, then grabs all the phone numbers associated with that lookup key.
Oh, related to this, the only names displayed in the contact list are ones where ContactsContract.Contacts.HAS_PHONE_NUMBER equals 1. So I know that all the contacts that can be selected have a phone number attached.
Facebook is not included in the ContactPicker because Facebook forbid that.
This is a politically thing and won't be solved soon: Google wants Facebook to share data, Facebook uses Google but doesn't share..
You'll have to use the Facebook SDK for android to do this. Use an FQL query to get the phone number.
I'm trying to retrieve the phone contacts having phone number starting with the number being dialled. For eample if I type 123, I would like to retrieve all the contacts having contact number starting with 123. I'm using the following code for this:
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursor = this.getContentResolver().query(
uri,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + dialledNumber + "%'", null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
The issue with this code is, if I have saved a contact like +919-9.... and another like +9199...., when I dial +9199 I can't retrieve both the contacts. I would like to escape the character "-" while querying the contacts. How could I do this? Please help. Thank you.
System.out.print("1-2-3".replaceAll("\\-", ""));
"My problem is not with the dialled number. Even if I dial 1234 or 123-4, I need all the contacts with phone number starting with 1234. But here if I type 1234 only contacts starting with 1234 is retrieved and not 123-4."
that is because you directly check for the number.try making your string dialledNumber such that it looks like 123-456-7890 then query for both this string like:
If suppose,
String dialledNumber="1234";
String dialledNumberFormatted="123-4"; // Create this on your own from dialledNumber you get
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursor = this.getContentResolver().query(
uri,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + dialledNumber + "%'" +" OR "+ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '" + dialledNumberFormatted + "%'", null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
This is a type of hack but you can use it doesn't cause problem for you anywhere else.