I have the following problem:
I have a RawContactID and want to get every information about this contact. I run a query like this:
String selection = ContactsContract.RawContactsEntity.RAW_CONTACT_ID +" = ?";
String[] selectionArgs = new String[]{contacts[0].get(0)};
Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, selection, selectionArgs, null);
After that I run the cursor through a lot of elseif-Statemens to distinguish betweene the different MIME-Types and read the informations. Works fine, besides that it is very much code only to read informations about one contact, but ok.
The problem is that the MIME-Type for Mobilephonenumber and Telephonenumber are the same (vnd.android.cursor.item/phone_v2)??
How can i distinguish between those to different informations?
Any hint is highly appreciated!
ContactsContract.CommonDataKinds.Phone will let you distinguish between the types. There is a long list of which TYPE_HOME and TYPE_MOBILE are just two.
The sdk reference also states
You can use all columns defined for ContactsContract.Data as well as the following aliases.
Related
I am trying to maintain a contact database and get a callback for Add/Update/Delete as soon as something changes in the URI.
I have written a ContentObserver to observe on ContactsContract.Contacts.CONTENT_URI on contacts. I get a callback as soon as a contact changes and then I update my database by checking ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP.
While this works fine for add/update, it does does not work for deleting a contact.
I do not want to parse all the contacts that I have in memory and check against android database. That would take time and CPU.
I know there exists many question of these types but I am not able to figure out things specific to deleting the contact.
Does there exist a way to perform this ?
As I have posted in above comment as well, following code works for API level 18 and above.
You can query on a uri ContactsContract.DeletedContacts.CONTENT_URI to get the list of all the contacts that have been deleted.
My query looks like following :
String selection = ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP + " > ?";
String[] selectionArgs = new String[]{String.valueOf(mLastContactDeleteTime)};
Cursor cursor = mContext.getContentResolver().query(ContactsContract.DeletedContacts.CONTENT_URI, null, selection, selectionArgs, null);
Is it possible to retrieve all the phone numbers with just one query(it's ok even if it's a bit slow)?
I found many topics that suggest to make query for the contact list and then a query for each contact but that's really slow.
The only solutions that I found with one query, retrieve just the main number. I need all the numbers of each contact plus the type.
Thanks.
try this
Cursor cursor = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, Phone.DISPLAY_NAME + " ASC");
I want to create a single Cursor that contains both given name, family name and phone nr. These columns are not located together in any of the containers available under ContactsContract and the only way I've been able to get this information is by first getting a cursor for the names and then getting phone numbers by creating a separate cursor for every contact. This solution forces me to read the data to a local data structure instead of just using an adapter on the cursor and imposes a lot of performance overhead (around ~5s with ~140 contacts with phone numbers).
Is there any way to create two cursors and then join the tables? Or is there any other way?
I have been struggling with this question for the last two days and read about everything I found on google, but really can't get anything working as I want too. This can't really be impossible, or is it?
Thanks in advance!
You can get the DISPLAY_NAME and NUMBER from ContactsContract.CommonDataKinds.Phone. Try this:
Uri uri = Phone.CONTENT_URI;
String[] projection = new String[]
{Phone.DISPLAY_NAME, Phone.NUMBER, Phone.CONTACT_ID, Phone._ID}
Cursor contactsCursor = getContentResolver().query(uri, projection, null, null, null);
I am a little bit confuse about the how to use Data.HAS_PHONE_NUMBER.
I have inserted contacts along with some additional Data and I would like to retrieve some of those contacts.
If I'm running the following code eveything is fine:
Uri uri = Data.CONTENT_URI;
String[] projection = new String[]{Data.CONTACT_ID, Data.DISPLAY_NAME };
String selection = Data.DATA4+" = ?";
String[] selectionArgs = new String[] {MY_VALUE};
Cursor truContactCursor = mContext.getContentResolver().query(uri, projection, selection, selectionArgs, null);
Now, I would like to know if those contacts have a phone number so I added HAS_PHONE_NUMBER into the projection like this:
String[] projection = new String[]{Data.CONTACT_ID, Data.DISPLAY_NAME, Data.HAS_PHONE_NUMBER };
When running the query I got the following execption:
java.lang.IllegalArgumentException: Invalid column has_phone_number
Why can I not access to HAS_PHONE_NUMBER? There is no problem with Data.DISPLAY_NAME which is in the same table. How are we suppose to use Data.HAS_PHONE_NUMBER then?
Thanks for your answer
Use HAS_PHONE_NUMBER is used for checking whether an contact has at least one phone number.example:
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cursor.getCount()>0){
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));id
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))>0){
//Query phone here.
}
or add in projection:
String [] projection = {ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER};
Add "Data.MIMETYPE=ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE" to the selection statement.
The result cursor will include only the rows which have a phone number.
Tip.
There is the case: several numbers in one contact.
If you want all the numbers to be queried, add "ContactsContract.Data.DATA1" (Which means phone number) to the projection statement.
I encountered this same problem, and the only other reference to the problem I have found is another stackoverflow question.
I am planning on writing my own ContentProvider to do the "join" through programmatic means. That is, do the original query against the Data.CONTENT_URI but without the Data.HAS_PHONE_NUMBER projection, and then requery the given contact for the actual value of Data.HAS_PHONE_NUMBER. This surely lacks the performance of an actual DB join (being an n+1-type of select), but will provide the desired/expected functionality. This extra processing could be made conditional upon the OS level in order to maintain the efficiency on correctly functioning OS versions.
Given yulistic's answer above and my own experience, it would appear to be a bug in some versions of Android that this implicit join is not done. My Android v2.3.7 phone crashes with the same error you receive when Data.HAS_PHONE_NUMBER is in the projection, whereas my Android v4.2.2 tablet does not crash.
I have entered an Android Bug against the issue:
https://code.google.com/p/android/issues/detail?id=54095
Rob
I have been trying to get all contacts of device sorted by the order they were added.
I am using following query:
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.CONTACT_STATUS_TIMESTAMP };
Cursor c = getContentResolver().query(uri, projection, null, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_STATUS_TIMESTAMP);
It is not making any difference whether I am passing null or ContactsContract.CommonDataKinds.Phone.CONTACT_STATUS_TIMESTAMP in the sort order of the query
After inspection, the cursor has null in all timestamp columns. Is there a way to get this done or what I am doing wrong in this?
That is the timestamp for the status update for the contact. You are getting null probably because none of the contacts have status updates.
Because of how contacts are aggregated, i don't think there is a way to find out which contact was added first because a contact's ID can actually change and appear to be added later than it originally was. Why do you want to figure this information out?