如何使用C#Xamarin删除android联系人

时间:2017-10-04 14:22:44

标签: xamarin.android

我一直试图让我的应用程序使用C#和Xamarin Forms从Android手机中删除联系人。我希望能够通过联系人的显示名称搜索联系人,然后将其从联系人中删除。

非常感谢任何帮助。

修改

我现在可以一次性删除手机中的所有联系人,但我仍然希望能够使用显示名称删除所选联系人以识别联系人。这是我的代码,我必须做出哪些更改才能删除所选联系人?

List<ContentProviderOperation> ops = new List<ContentProviderOperation>();

ops.Add(ContentProviderOperation.NewDelete(ContactsContract.RawContacts.ContentUri).Build());

Android.App.Application.Context.ContentResolver.ApplyBatch(ContactsContract.Authority, ops);

1 个答案:

答案 0 :(得分:0)

因此经过大量工作后,这是我的解决方案。 ToDelete是我在比较联系人的显示名称时传递的字符串。

Context thisContext = Android.App.Application.Context;

            string[] Projection = new string[] { ContactsContract.ContactsColumns.LookupKey, ContactsColumns.DisplayName };

            ICursor cursor = thisContext.ContentResolver.Query(ContactsContract.Contacts.ContentUri, Projection, null, null, null);

            while (cursor != null & cursor.MoveToNext())
            {
                string lookupKey = cursor.GetString(0);
                string name = cursor.GetString(1);

                if (name == ToDelete)
                {
                    var uri = Uri.WithAppendedPath(ContactsContract.Contacts.ContentLookupUri, lookupKey);
                    thisContext.ContentResolver.Delete(uri, null, null);
                    cursor.Close();
                    return;
                }
            }