获取删除的联系人有时返回null

时间:2017-03-24 10:13:27

标签: android contacts

我需要在android中检索已删除的联系人。以下是我的代码

String WHERE_MODIFIED = "( "+ ContactsContract.RawContacts.DELETED + "= 1 )";

Cursor c = getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI,
        null,
        WHERE_MODIFIED,
        null,
        null);
if (c.getCount() > 0) {
  c.moveToFirst();
  do{
      String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
      Log.e(TAG,name);
  }while (c.moveToNext());
}else {
    Log.e(TAG,"cursor = null");
}

当我使用上面的代码删除联系人和获取时它起作用但在删除一段时间后它返回null。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

Cursor cursor = createDeletedCursor(contactLastUpdatedTime); 


private Cursor createDeletedCursor(long lastupdatedTime) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        return resolver.query(
                ContactsContract.DeletedContacts.CONTENT_URI,
                DELETED_PROJECTION,
                String.format("%s >= %d", ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP, lastupdatedTime),
                null,
                ContactsContract.DeletedContacts.CONTACT_ID
        );
}