简体   繁体   English

Xamarin.forms设备联系人未更新

[英]Xamarin.forms Device contact is not getting updated

I have a Xamarin.Forms based application which uses operation with device contact like adding new contact and updating existing contact. 我有一个基于Xamarin.Forms的应用程序,它使用设备联系人的操作,例如添加新联系人和更新现有联系人。 Adding new contacts works, but update contact is not working. 添加新联系人有效,但是更新联系人无效。 I used DependencyService of Xamarin.forms to update contacts on android. 我使用Xamarin.forms DependencyService来更新android上的联系人。 First i lookup for that contact and get its' Id if it exist. 首先,我查找该联系人并获取其“ ID(如果存在)。 Below is my code 下面是我的代码

void OnUpdate(){
 var Id = LookupByPhone(phoneNumber,   companyName+ " " + teamName);
        if(Id!="0")
        {
            UpdateContact(phoneNumber, Id,companyName,teamName);
            return;
        }
    }

string LookupByPhone(string phoneNumber, string query = "")
    {
        var Id = "0";
        try
        {
            var context = MainActivity.Instance;

            var uri = ContactsContract.Contacts.ContentUri;
            uri = ContactsContract.CommonDataKinds.Phone.ContentUri;

            string[] projection = {
                ContactsContract.Contacts.InterfaceConsts.Id,

                InterfaceConsts.DisplayName,
                ContactsContract.CommonDataKinds.Phone.Number
                };

            var cursor = context.ContentResolver.Query(uri, projection, null, null, null);

            if (cursor.MoveToFirst())
            {
                do
                {
                    var id = cursor.GetString(cursor.GetColumnIndex(projection[0]));
                    var nm = cursor.GetString(cursor.GetColumnIndex(projection[1]));
                    var number = cursor.GetString(cursor.GetColumnIndex(projection[2]));

                    Id = id;
                    if (nm == query)
                        break;

                    //break;
                } while (cursor.MoveToNext());
            }
            cursor.Close();
        }
        catch (Exception ex)
        {
            LogController.LogError(ex);
        }
        return Id;
    }



void UpdateContact(string phonenumber, string id, string givenName, string familyName)
    {
        ContentProviderOperation.Builder builder = ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);


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

        if (!string.IsNullOrWhiteSpace(givenName) && !string.IsNullOrWhiteSpace(familyName))
        {
            // Name 
            String nameSelection = ContactsContract.Data.InterfaceConsts.ContactId + " = ? AND "
                                   + ContactsContract.Data.InterfaceConsts.Mimetype + " = ? ";
            String[] nameSelectionArgs = {
                                            id.ToString(),
                                            ContactsContract.CommonDataKinds.StructuredName.ContentItemType
            };


            builder.WithSelection(nameSelection, nameSelectionArgs);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.GivenName, givenName);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.FamilyName, familyName);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.DisplayName, givenName + " " + familyName);

            ops.Add(builder.Build());
        }

        #region Phone update
        // CellPhone
        String phoneSelection = ContactsContract.Data.InterfaceConsts.ContactId + " = ? AND " +
                                   ContactsContract.Data.InterfaceConsts.Mimetype + " = ? AND " +
                                   ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type + " = ?";

        String[] phoneselectionArgs = {
               id.ToString(),
                   ContactsContract.CommonDataKinds.Phone.ContentItemType,
               PhoneDataKind.Mobile.ToString()
            };

        builder = ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);
        builder.WithSelection(phoneSelection, phoneselectionArgs);

        builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, phonenumber);
        builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type, PhoneDataKind.Mobile.ToString());
        ops.Add(builder.Build());
        #endregion

        // Update the contact
        ContentProviderResult[] result;
        try
        {
        // the result return count{0}                
        var result = MainActivity.Instance.ContentResolver.ApplyBatch(ContactsContract.Authority, ops);

        }
        catch (Exception ex)
        {
            LogController.LogError("Error updating phone:" + phonenumber,ex);
        }
    }

There are no error or exception. 没有错误或异常。 Any thought on this. 任何想法。

The method which gets the id should be like this (I removed the first parameter since it's not used in your method): 获取id的方法应该是这样的(我删除了第一个参数,因为在您的方法中未使用它):

string LookupByPhone(string name)
{
    string id = "0";
    var uri = ContactsContract.Contacts.ContentUri;
    var cursor = this.ContentResolver.Query(
                 uri,
                 new String[] { ContactsContract.Contacts.InterfaceConsts.Id },
                 ContactsContract.Contacts.InterfaceConsts.DisplayName +
                 "='" + name + "'", null, null);
    if (cursor.MoveToNext())
    {
        id = cursor.GetString(cursor.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.Id));
    }
    cursor.Close();
    return id;
 }

You can see I get the id by query the name that is what you are using companyName + " " + teamName 您可以通过查询使用companyName + " " + teamName的名称来获取ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Xamarin.Forms 上获取真正的 iPhone 设备 IMEI - Getting real iPhone device IMEI on Xamarin.Forms IOS 联系邮政地址 Xamarin.Forms - IOS contact postal address in Xamarin.Forms Xamarin.Forms中Xaml中的Device.OS - Device.OS in Xaml in Xamarin.Forms 在 Xamarin.Forms iOS 中,获取 HttpRequestException:锁定设备时发送请求时出错 - In Xamarin.Forms iOS, Getting HttpRequestException: An error occurred while sending the request when locking the device xamarin.forms和Microsoft Azure入门 - Getting started with xamarin.forms and Microsoft Azure Xamarin.Forms - iOS RegisteredForRemoteNotifications 没有被调用 - Xamarin.Forms - iOS RegisteredForRemoteNotifications not getting called 选择器未在Xamarin.Forms应用中显示 - Picker is not getting displayed in Xamarin.Forms app Xamarin.Forms - 根据设备分辨率更改设备方向 - Xamarin.Forms - change device orientation based on device resolution Xamarin.Forms Android 在将图像保存到设备库时获取访问被拒绝异常而未在设备中启用存储权限 - Xamarin.Forms Android when save the image to device gallery getting access denied exception without enable storage permission in device 如何在Xamarin.Forms中获取客户端设备的MAC地址? - How to get MAC address of a client device in Xamarin.Forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM