Search code examples
androidxamarinxamarin.androidandroid-contactsandroid-contentresolver

Context.ContentResolver.Delete returns 0 for some contact


I have a strange issue. I am trying to delete contacts based on given phone and/or email address from device. I had 150 contacts in device, so it deleted around 100 contacts but rest 50 are not deleting.

I use following code to delete the contact(this method is calling from a loop).

public bool DeleteRecord(string phoneNumber, string email)
{
    var Id = "0";
    var isDeleted = false;
    string lookupKey = string.Empty;
    try
    {

    var uri = ContactsContract.Contacts.ContentUri;
    string[] projection = {
        InterfaceConsts.Id,
        InterfaceConsts.DisplayName,
        ContactsContract.Contacts.InterfaceConsts.LookupKey
        };
    var uri1 = Android.Net.Uri.WithAppendedPath(ContactsContract.PhoneLookup.ContentFilterUri, Android.Net.Uri.Encode(phoneNumber));

    var cursor = appContext.ContentResolver.Query(uri1, projection, null, null, null);


    if (cursor.MoveToFirst())
    {
        do
        {
            Id = cursor.GetString(cursor.GetColumnIndex(projection[0]));

            if (Id != "0")
            {
                int idx = cursor.GetColumnIndex(ContactsContract.ContactsColumns.LookupKey);
                lookupKey = cursor.GetString(idx);
            }
            break;
        } while (cursor.MoveToNext());
        cursor.Close();
    }
    isDeleted = Id != "0";
    if (isDeleted)
    {
        var u = Android.Net.Uri.WithAppendedPath(ContactsContract.Contacts.ContentLookupUri, lookupKey);
        var res = appContext.ContentResolver.Delete(u, "contact_id=" + Id, null);
        return true;
    }
}
catch (Exception ex)
{
    Android.Util.Log.Error("Error:", ex.Message + " " + ex.StackTrace);
}


return false;
}

I see an error in "Notification bar" of device: "Too many contact deletions"

So not sure what is going wrong here. Any thoughts on this?


Solution

  • I am trying to delete all contacts from device

    your code will not delete all contacts from the device, it's based on querying by phone number, so all contacts that don't have a phone number will not get deleted.

    Also, there's a much easier way to delete all contacts from device, no need to run a query and delete calls per contacts, just one, this is not tested, but should work, as the selection will match all contacts on the device.

    ContentResolver cr = getContentResolver();
    cr.delete(ContactsContract.Contacts.CONTENT_URI, null, null);
    

    (It's in Android Java API, you'll need to convert to Xamarin APIs, as I'm less familiar with them)

    I see an error in "Notification bar" of device: "Too many contact deletions"

    A SyncProvider is in charge of syncing server changes to the cloud, and cloud changes to the device. When a SyncProvider encounters lots of client-side deletions, it raises a flag, and before it syncs those deletions to the cloud it'll inform the user via a notification, clicking the notification should ask the user if that was intentional, and what he/she wants to do now: sync the deletions to the cloud, undo all deletion, do nothing (keep the deletions local, but don't sync them to the cloud).