Android delete specific contact programmatically

Now this code works ops.add(ContentProviderOperation .newDelete(ContactsContract.Data.CONTENT_URI) .withSelection(ContactsContract.Data.RAW_CONTACT_ID+"=?",new String[] {sid}).build());

However, he creates an unknown record and it seems that its remote contact. Do I need something to work correctly?

+6
source share
3 answers

I did it like this:

 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, 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, null); return true; } } while (cur.moveToNext()); } } catch (Exception e) { System.out.println(e.getStackTrace()); } return false; } 
+7
source

As explained in

http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html

The CONTACT_ID field in RawContacts is a link to an aggregated contact. Removing RawContact with ContentProviderOperation.newDelete will make this field null (as I found). When RawContact is actually removed, it depends on the synchronization and aggregation of Android. I needed to delete a specific contact from a specific account, so deleting from RawContacts rather than aggregated contacts worked for me. To check if RawContact was removed, I used:

  ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ContentResolver contentResolver = getContentResolver(); Cursor curRawContacts = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI, null, ContactsContract.RawContacts.ACCOUNT_TYPE + " = ? AND " + ContactsContract.RawContacts.ACCOUNT_NAME + " = ?", new String[]{"MYTYPE", MyAccount}, null); int size = curRawContacts.getCount(); for (int i = 0; i < size; i++) { curRawContacts.moveToPosition(i); //getColumnIndexOrThrow(String columnName) //Returns the zero-based index for the given column name, or throws IlleidRawContactgalArgumentException if the column doesn 't exist. String idRawContact = curRawContacts.getString(curRawContacts.getColumnIndexOrThrow(ContactsContract.RawContacts._ID)); String idContact = curRawContacts.getString(curRawContacts.getColumnIndexOrThrow(ContactsContract.RawContacts.CONTACT_ID)); // if link to aggregated contact is null, than raw contact had already been deleted if (idContact != null) { 
0
source

newDelete method with ContentProvider does not work for me . It deletes all the information in the contact, but leaves the contact empty (null null) in your phonebook.

This is why I had to use ContentResolver with the delete method.

I wanted to delete contacts created using my application using SOURCE_ID .

I set CALLER_IS_SYNCADAPTER to true .

So this is how I dealt with this:

 Uri contactUri = RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(); String whereClause = RawContacts.SOURCE_ID + " = ?"; String[] args = new String[]{contactId}; int deletedRawContacts = context.getContentResolver().delete(contactUri, whereClause, args); if (deletedRawContacts > 0) { Log.d(TAG, "Delete OK"); } else { Log.d(TAG, "Nothing to delete"); } 
0
source

Source: https://habr.com/ru/post/917899/


All Articles