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

  • Question

  • User377360 posted

    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 ACCOUNTTYPE and ACCOUNTNAME were inside RawContacts.InterfaceConsts.

    But I cannot seem to find the ContactsContract.RawContacts.AGGREGATIONMODEDISABLED 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.AGGREGATIONMODEDISABLED in Xamarin.Android?

    Saturday, June 15, 2019 1:11 PM

All replies

  • User379860 posted

    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@nomail.com";
            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" />
    
    Monday, June 17, 2019 7:09 AM
  • User379860 posted

    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.

    Wednesday, June 19, 2019 9:06 AM