现在,这段代码正在运行
ops.add(ContentProviderOperation .newDelete(ContactsContract.Data.CONTENT_URI).withSelection(ContactsContract.Data.RAW_CONTACT_ID“=?”,new String [] {sid}).build());
ops.add(ContentProviderOperation .newDelete(ContactsContract.Data.CONTENT_URI).withSelection(ContactsContract.Data.RAW_CONTACT_ID“=?”,new String [] {sid}).build());
但它创建了未知记录,似乎是已删除的联系人.我需要一些能让它正常工作的东西吗?
解决方法
我这样做了:
public static boolean deleteContact(Context ctx,String phone,String name) { Uri contactUri = Uri.withAppendedpath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phone)); Cursor cur = ctx.getContentResolver().query(contactUri,null,null); try { if (cur.movetoFirst()) { do { if (cur.getString(cur.getColumnIndex(PhoneLookup.disPLAY_NAME)).equalsIgnoreCase(name)) { String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedpath(ContactsContract.Contacts.CONTENT_LOOKUP_URI,lookupKey); ctx.getContentResolver().delete(uri,null); return true; } } while (cur.movetoNext()); } } catch (Exception e) { System.out.println(e.getStackTrace()); } return false; }