如何删除以特定名称开头的所有 android 个联系人?
How to delete all android contacts that start with a specific name?
我想从 android phone 中删除所有以 "AAA" 开头或包含 "AAA" 的联系人。这是我尝试过的:
private void deleteContact(String name) {
ContentResolver cr = getContentResolver();
String where = ContactsContract.Data.DISPLAY_NAME + " = ? ";
String[] params = new String[] {"AAAA"};
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI)
.withSelection(where, params)
.build());
Log.e(",,,,",String.valueOf(ops.get(0)));
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(NativeContentProvider.this, "Deleted the contact with name '" + name +"'", Toast.LENGTH_SHORT).show();
}
但失败了。请给我一些想法,以便我可以继续我的项目。
也许这会让您走上正轨:
ContentResolver mContentResolver = getContentResolver();
private int deleteContactsLike(String name) {
return mContentResolver.delete(
ContactsContract.RawContacts.CONTENT_URI,
ContactsContract.Contacts.DISPLAY_NAME
+ " like ?",
new String[] { name + '%'});
我想从 android phone 中删除所有以 "AAA" 开头或包含 "AAA" 的联系人。这是我尝试过的:
private void deleteContact(String name) {
ContentResolver cr = getContentResolver();
String where = ContactsContract.Data.DISPLAY_NAME + " = ? ";
String[] params = new String[] {"AAAA"};
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI)
.withSelection(where, params)
.build());
Log.e(",,,,",String.valueOf(ops.get(0)));
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(NativeContentProvider.this, "Deleted the contact with name '" + name +"'", Toast.LENGTH_SHORT).show();
}
但失败了。请给我一些想法,以便我可以继续我的项目。
也许这会让您走上正轨:
ContentResolver mContentResolver = getContentResolver();
private int deleteContactsLike(String name) {
return mContentResolver.delete(
ContactsContract.RawContacts.CONTENT_URI,
ContactsContract.Contacts.DISPLAY_NAME
+ " like ?",
new String[] { name + '%'});