Android- Fetch the contacts based on contact id greater than some value - android

i want to fetch the contacts from android periodically. for that i
want to something like this. i am taking all the conatcts from the
android and store it in my local db. so i will store last contact id.
so next time on wards if any contacts added , i need to get those
contacts only so for that purpose i want to fetch the contact id
greater than last time fetched contact id. how can write the query for
content resolver to fetch the contacts some thing like.
Select * from tablename where id > 100;

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null, // this will be the columns selection
ContactsContract.Contacts._ID + ">100", // this is where you add the "where" part of the query, id is 1 in this case
null, // where args, you are skipping these
null); // sort order
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
long id = Long.parseLong(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).trim();
}
}
Hope this solves your issue

Related

Unable to fetch deleted contacts from ContactsContract.Contacts.CONTENT_URI

I am trying to fetch deleted contacts from Android contacts table. After searching some Stack Overflow links, best way to fetch the deleted contacts is by using ContactsContract.DeletedContacts. I am able to fetch the deleted contacts from DeletedContacts table.
val deletedContactsProjection = arrayOf(ContactsContract.DeletedContacts.CONTACT_ID,ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP)
val contactLastDeletedTimeStamp = SharedPreferenceManager.instance.getLongFromPreference("contact_sync_last_updated_timestamp")
val deletedContactSelection = ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP + " > ?";
val deletedContactSelectionArgs = arrayOf<String>(contactLastDeletedTimeStamp.toString())
var deletedContactsCursor = context.contentResolver.query(ContactsContract.DeletedContacts.CONTENT_URI, deletedContactsProjection,deletedContactSelection, deletedContactSelectionArgs,null)
Using the above cursor I'm able to fetch the last deleted contact. When I checked the ContactsContract.DeletedContacts.CONTACT_ID of the deleted contact from cursor, it's same as the contact id which I deleted. But the problem is when I'm trying to fetch the contact from ContactsContract.Contacts table using the ContactsContract.DeletedContacts.CONTACT_ID its always returning null.
This is the query I'm using:
String[] contactNumberProjection = {ContactsContract.Contacts._ID};
String contactId = deletedContactsCursor.getString(deletedContactsCursor.getColumnIndex(ContactsContract.DeletedContacts.CONTACT_ID));
String contactSelection = ContactsContract.Contacts._ID + " = ?";
String[] contactSelectionArg = {contactId};
Cursor contactCursor =
context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,contactNumberProjection,contactSelection,contactSelectionArg,null);
Where am I going wrong?
When a contact is deleted from the main Contacts table it is obviously removed from that table so that the contact will not appear in the Contacts apps the user is using the view their present contacts.
So you can only get details of deleted contacts in the DeletedContacts table.

How could I get the maximum the contact id in contact by programming?

I want to insert a new phone number with new contact id in the my SQLite. I need to know the maximum contact id in my contact, to ignore the saving same contact id. Is it possible to know the maximum contact id in Android? Thank you
From Contacts Table , you can get the last Contact Id by querying like shown below
final String sortOrder = ContactsContract.Contacts._ID+" DESC LIMIT 1";
Cursor lcursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,new String[] {ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME},null,null,sortOrder);
if (lcursor != null && lcursor.moveToFirst()) {
long id = lcursor.getLong(lcursor.getColumnIndex(ContactsContract.Contacts._ID));
String displayName = lcursor.getString(lcursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
Cursor will return only one entry and that entry will be the last contact_id in the contact's table
Note: This should not be done on Main thread, as it may take too much time if device Contact is more 1000+ contacts, so always be carefull of Querying the DataBase having large entries
Enjoy coding ! :)

getting notes from a single contact - android

I'm trying to get "notes" from a single contact. It added fine but retrieving it has been a problem.
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'%" + sender +"%'";
String[] projection = new String[] { ContactsContract.CommonDataKinds.Note.NOTE};
Cursor c2 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, null, null);
if (c2.moveToFirst()) {
notes = c2.getString(0);
}
It works fine with other values like name or phone number but can't seem to get notes to retrieve correctly. It retrieves a random value like email instead.
I believe that your problem is that not all rows in the table represent contact types that have notes. You have to request the proper MIME Type.
ContactsContract.CommonDataKinds.Note is an alias for the 'data1' column that is present on all rows, so when you get a row of a different MIME Type, it represents different data.
How to get contacts in Android should give you an idea of how to do this.

Android: Get CallLog History from a certain contact

I query the CallLog.Calls provider in order to retrieve a list of calls from a certain contact, based on the contact's display name. In particular, I use this query:
String selection = CallLog.Calls.CACHED_NAME + "= ?";
String dispName = dataCollector.getDisplayName();
Cursor callCursor =
cr.query(callLogUri, callLogProjection, selection,
new String[] {dispName},CallLog.Calls.DATE + " DESC");
The dataCollector object is used to hold information from queries based on a given contact id.
The problem is that this code only returns one call for the given contact. I can't understand why. Any clues?
int i=0;
while(cursor.moveToNext())
{
Sring id = cursor.getString(cursor.getColumnIndex(CallLog.Calls._ID));
numbersTemp[i]=cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
valuesTemp[i]=cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
i++;
}

how to get phone numbers for particular group

I'm working on Android contact. I want to query phone numbers (not contact name) from a specific group name. What query should i perform in order to do this?
Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI
, null, ContactsContract.Data.MIMETYPE+"=?"
, new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE}
, null);
Then loop through cursor and get data u want. This will return data blocks showing the contactID and the groupID and other info. With this then query ContactsContract.Groups and get data about the group to compare.
If you are looking for specific data about a group first query for group row ID than you can add that to the following cursor like so...
Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI
, null, ContactsContract.Data.MIMETYPE+"=? AND "+ ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID+"=?"
, new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE, rowID }
, null);
wrote code here so sorry for silly mistakes
You can find group id like so...
Cursor c = context.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, new String[]{ContactsContract.Groups._ID}, ContactsContract.Groups.TITLE+"=?","myGroup", null);

Categories

Resources