【问题标题】:How to delete an android contact using C# Xamarin如何使用 C# Xamarin 删除 android 联系人
【发布时间】:2017-10-04 14:22:43
【问题描述】:

我一直在尝试让我的应用使用 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);

【问题讨论】:

  • 在原生Android中需要使用DependencyService来实现这个功能。
  • 我有一个用于从手机添加和获取联系人的依赖服务,但我不知道如何删除选定的联系人。

标签: xamarin.android


【解决方案1】:

因此,经过大量工作,这是我的解决方案。 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;
                }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多