How to get the value of ContactsContract.RawContacts.AGGREGATION_MODE_DISABLED in xamarin?

I am trying to insert contacts programatically in Xamarin.Android.

I have found this Java code and was trying to convert it to Xamarin C#
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation .newInsert(ContactsContract.RawContacts.CONTENT_URI) .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) .withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DISABLED).build());

Usually the difference with the constants of RawContacts is that in Xamarin C#, constants were using pascal casing. The ACCOUNT_TYPE and ACCOUNT_NAME were inside RawContacts.InterfaceConsts.

But I cannot seem to find the ContactsContract.RawContacts.AGGREGATION_MODE_DISABLED in Xamarin.Android. I did discover from the Java Android documentation that its value is 3 but I don't want to hard code it. So where to find the value of ContactsContract.RawContacts.AGGREGATION_MODE_DISABLED in Xamarin.Android?

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    You can use Android.Provider.AggregationMode.Disabled to achieve that.

    You can use my code to insert a contacts.

            string lastName = "Leon ";
            string firstName = "Lu";
            string phone = "123456";
    
            string email = "[email protected]";
            string company = "bad";
    
    
            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);
            builder.WithValue(ContactsContract.RawContacts.InterfaceConsts.AggregationMode, 
            AggregationMode.Disabled.GetHashCode());
            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.FamilyName, lastName);
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.GivenName, firstName);
            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, phone);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.Phone.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Label, "Work");
            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, email);
            builder.WithValue(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.Email.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Label, "Work");
            ops.Add(builder.Build());
    
            //Company
            builder = ContentProviderOperation.NewInsert(ContactsContract.Data.ContentUri);
            builder.WithValueBackReference(ContactsContract.Data.InterfaceConsts.RawContactId, 0);
            builder.WithValue(ContactsContract.Data.InterfaceConsts.Mimetype,
                              ContactsContract.CommonDataKinds.Organization.ContentItemType);
            builder.WithValue(ContactsContract.CommonDataKinds.Organization.InterfaceConsts.Data, company);
            builder.WithValue(ContactsContract.CommonDataKinds.Organization.InterfaceConsts.Type,
                              ContactsContract.CommonDataKinds.Organization.InterfaceConsts.TypeCustom);
            builder.WithValue(ContactsContract.CommonDataKinds.Organization.InterfaceConsts.Label, "Work");
            ops.Add(builder.Build());
    
            //Add the new contact
            ContentProviderResult[] res;
            try
            {
                res = this.ContentResolver.ApplyBatch(ContactsContract.Authority, ops);
    
                Toast.MakeText(this, "success", ToastLength.Short).Show();
            }
            catch
            {
                Toast.MakeText(this, "fail", ToastLength.Long).Show();
            }
        }
    

    In the end, Do not forget to add these persmission.

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    
  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Are there any updates for this issue? If the reply is helpful, please try to mark it as an answer, it will help others who have similar issue.

Sign In or Register to comment.