I did following code for deleting selected contacts from sim card. but its not deleting and also not throwing any error.
protected void DeleteContacts(ArrayList<String> ids){
int flg = 0;
String[] strids = new String[ids.size()];
strids = ids.toArray(strids);
for (int i = 0; i < strids.length; i++) {
Cursor sims = getActivity().getContentResolver().query(
Uri.parse("content://icc/adn"), null,
"_id=?", new String[]{strids[i]}, null);
sims.moveToFirst();
if (sims.getCount()>0) {
String phoneNumber = sims.getString(sims.getColumnIndex("number"));
boolean val = deleteContact(phoneNumber);
if (!val)
flg=1;
}
if (flg == 0)
Toast.makeText(getActivity(), "Contact Deleted", Toast.LENGTH_SHORT).show();
sims.close();
}
}
public boolean deleteContact(String phone) {
Cursor cur = getActivity().getContentResolver().query(Uri.parse("content://icc/adn"), null, "number=?", new String[] { phone }, null);
try {
if (cur.moveToFirst()) {
do {
String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
getActivity().getContentResolver().delete(uri, null, null);
return true;
} while (cur.moveToNext());
}
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
return false;
}