Jeeva
Jeeva

Reputation: 1961

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?

Upvotes: 0

Views: 284

Answers (1)

marmor
marmor

Reputation: 28209

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.

Upvotes: 1

Related Questions