所以基本上我有一个删除手机联系人的功能:
String[] args = new String[]{number};
try {
ArrayList ops = new ArrayList();
// if id is raw contact id
ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).withSelection(ContactsContract.RawContacts._ID + "=?", args).build());
// if id is contact id
// ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).withSelection(ContactsContract.RawContacts.CONTACT_ID + "=?", args).build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
Toast.makeText(this, "Contact Deleted", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("$$$$", "ERRORDELETE#133");
}
我尝试了两种选择,如果原始联系人或联系人ID,两者都没有工作/
当我运行APP并且该功能激活了“已删除的联系人”#39;显示并在日志中:
05-24 15:40:16.444 6540-6540/com.assistme.meirovichomer.assistme E/ViewRootImpl: sendUserActionEvent() mView == null
05-24 15:40:16.464 6540-6679/com.assistme.meirovichomer.assistme V/RenderScript: Application requested CPU execution
05-24 15:40:16.474 6540-6679/com.assistme.meirovichomer.assistme V/RenderScript: 0xa14a7e00 Launching thread(s), CPUs 4
我不确定我是否只是不理解日志或者我错过了什么,但是当我在电话上输入联系人列表时,联系人仍然没有,因此没有被删除。很想得到一些帮助,非常感谢您提前!
答案 0 :(得分:0)
试试这个。
删除课程。
public static void deleteContact(ContentResolver contactHelper, String
number) {
ArrayList<ContentProviderOperation> ops = new
ArrayList<ContentProviderOperation>();
String[] args = new String[] { String.valueOf(getContactID(contactHelper,
number))};
ops.add(ContentProviderOperatio.newDelete(RawContacts.CONTENT_URI).withSelecti
on(RawContacts.CONTACT_ID + "=?", args).build());
try {
contactHelper.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
}
和getid class
private static long getContactID(ContentResolver contactHelper,String
number) {
Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] projection = { PhoneLookup._ID };
Cursor cursor = null;
try {
cursor = contactHelper.query(contactUri, projection, null, null,null);
if (cursor.moveToFirst()) {
int personID = cursor.getColumnIndex(PhoneLookup._ID);
return cursor.getLong(personID);
}
return -1;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
cursor = null;
}
}
return -1;
}