Android(Xamarin)更新联系人

时间:2016-12-04 10:35:19

标签: c# android xamarin xamarin.android

我在更新联系人时遇到了一些麻烦 - 昨天我设法将我在测试手机上的所有联系方式更改为" 0 Main" : - (

我可以使用以下代码创建一个全新的联系人:

if (_IsNewContact)
        {
            List<ContentProviderOperation> ops = new List<ContentProviderOperation>();

            ContentProviderOperation.Builder builder =
                ContentProviderOperation.NewInsert(ContactsContract.RawContacts.ContentUri);
            builder.WithValue(ContactsContract.RawContacts.InterfaceConsts.AccountType, null);
            builder.WithValue(ContactsContract.RawContacts.InterfaceConsts.AccountName, null);
            ops.Add(builder.Build());

            //Name
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, 0);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.StructuredName.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.GivenName, _Contact.FirstName);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.FamilyName, _Contact.LastName);
            ops.Add(builder.Build());

            //Number
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, 0);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Phone.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, _Contact.Phone);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.Phone.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Label, "Main");
            ops.Add(builder.Build());

            //Email
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, 0);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Email.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Data, _Contact.Email);
            builder.WithValue(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.Email.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Label, "Main");
            ops.Add(builder.Build());

            //Add the new contact
            ContentProviderResult[] result;

            try
            {
                result = ContentResolver.ApplyBatch(ContactsContract.Authority, ops);
            }
            catch { }
        }

以下是我用来更新的代码,但此刻它会尝试更改每个联系人,将所有值更改为0 Main

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

            ContentProviderOperation.Builder builder =
                ContentProviderOperation.NewUpdate(ContactsContract.RawContacts.ContentUri);
            builder.WithValue(ContactsContract.RawContacts.InterfaceConsts.AccountType, null);
            builder.WithValue(ContactsContract.RawContacts.InterfaceConsts.AccountName, null);
            ops.Add(builder.Build());

            //Name
            builder = ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, 0);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.StructuredName.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.GivenName, _Contact.FirstName);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.FamilyName, _Contact.LastName);
            ops.Add(builder.Build());

            //Number
            builder = ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, 0);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Phone.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, _Contact.Phone);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.Phone.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Label, "Main");
            ops.Add(builder.Build());

            //Email
            builder = ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, 0);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Email.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Data, _Contact.Email);
            builder.WithValue(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.Email.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Label, "Main");
            ops.Add(builder.Build());

            //Add the new contact
            ContentProviderResult[] result;

            try
            {
                result = ContentResolver.ApplyBatch(ContactsContract.Authority, ops);
            }
            catch { }
        }

1 个答案:

答案 0 :(得分:2)

以下代码允许您使用Xamarin原生android更新联系人。

            // Name 
            String nameSelection = ContactsContract.Data.InterfaceConsts.RawContactId + " = ? AND " 
                                   + ContactsContract.Data.InterfaceConsts.Mimetype + " = ? ";
            String[] nameSelectionArgs = {
                _Contact.DataId.ToString(),
                ContactsContract.CommonDataKinds.StructuredName.ContentItemType
            };

            ContentProviderOperation.Builder builder = ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);
            builder.WithSelection(nameSelection, nameSelectionArgs);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.GivenName, _Contact.FirstName);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.FamilyName, _Contact.LastName);
            ops.Add(builder.Build());

            // Phone
            String phoneSelection = ContactsContract.Data.InterfaceConsts.RawContactId + " = ? AND " 
                                    + ContactsContract.Data.InterfaceConsts.Mimetype + " = ? ";
            String[] phoneelectionArgs = {
                _Contact.DataId.ToString(),
                ContactsContract.CommonDataKinds.Phone.ContentItemType
            };

            builder = ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);
            builder.WithSelection(phoneSelection, phoneelectionArgs);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.Number, _Contact.Phone);
            ops.Add(builder.Build());

            // Email
            String emailSelection = ContactsContract.Data.InterfaceConsts.RawContactId + " = ? AND "
                             + ContactsContract.Data.InterfaceConsts.Mimetype + " = ? ";
            String[] emailSelectionArgs = {
                _Contact.DataId.ToString(),
                ContactsContract.CommonDataKinds.Email.ContentItemType
            };

            builder = ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);
            builder.WithSelection(emailSelection, emailSelectionArgs);
            builder.WithValue(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Data, _Contact.Email);
            ops.Add(builder.Build());

            // Update the contact
            ContentProviderResult[] result;
            try
            {
                result = ContentResolver.ApplyBatch(ContactsContract.Authority, ops);
            }
            catch { }