如何从android手机中获取已删除的联系人,以及如何恢复



我正在这样做,但这不是为我获取电话号码。我正在使用竞争提供商,我想从我的手机中获取已删除的联系人并将其恢复到我的手机。提前谢谢。

public static final String WHERE_MODIFIED1 = "( "+ ContactsContract.RawContacts.DELETED + "=1)";
public void readContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query((ContactsContract.RawContacts.CONTENT_URI),
null, WHERE_MODIFIED1, null, null);
assert cur != null;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String phone = null;
//if (!(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)) {
System.out.println("name : " + name + ", ID : " + id);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("phone" + phone);
}
pCur.close();
//}
if (phone != null) {
contactList.add(new Contact(name, phone));
}
Log.e("hvy", "onCreaterrView  Phone Number: name = " + name
+ " No = " + phone);
}
}
cur.close();
}

我的日志显示,如果我运行这个代码

E/hvy: onCreaterrView  Phone Number: name = Abc No = null
I/System.out: name : Abc, ID : 4090
E/hvy: onCreaterrView  Phone Number: name = Abc No = null
I/System.out: name : Abc, ID : 4091
E/hvy: onCreaterrView  Phone Number: name = Abc No = null
I/System.out: name : Gagaga, ID : 4092
E/hvy: onCreaterrView  Phone Number: name = Gagaga No = null
I/System.out: name : Shah, ID : 4093
E/hvy: onCreaterrView  Phone Number: name = Shah No = null
I/System.out: name : CardRecharge, ID : 4105

只需将您要获得的id传递到代码下方

ArrayList arrayList = new ArrayList();
arrayList.add(ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter("caller_is_syncadapter", "true").build()).withSelection("_id=?", new String[]{String.valueOf(id)}).withValue("deleted", 0).withYieldAllowed(true).build());
try {
YourActivity.this.getContentResolver().applyBatch("com.android.contacts", arrayList);
} catch (OperationApplicationException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}

最新更新