Xamarin.forms设备联系人未获得更新

时间:2017-12-06 19:44:31

标签: c# xamarin.forms xamarin.android

我有一个基于Xamarin.Forms的应用程序,它使用与设备联系的操作,例如添加新联系人和更新现有联系人。添加新联系人有效,但更新联系人无效。我使用DependencyService Xamarin.forms来更新android上的联系人。首先,我查找该联系人并获取其联系人。我是否存在。 以下是我的代码

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);
        }
    }

没有错误或例外。对此有任何想法。

1 个答案:

答案 0 :(得分:0)

获取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;
 }

您可以通过查询使用companyName + " " + teamName

的名称来查看我的ID