Update a contact - Maui Android/Ios app

john murray 16 Reputation points
2024-03-29T11:23:42.39+00:00

I have an app which is to run on Ios and Android. I use VS 2022 17.9.4

app for Android (which is my problem right now) targets And33 with min ver 23

The maui helper "Microsoft.Maui.ApplicationModel.Communication.Contacts.Default.PickContactAsync();"

works fine but since the demise of Maui.essentials it is no longer possible to update and save a contact. I am concentrating on Android here - I have discovered numerous 'answers' on the net which say this code should work - but it doesnt either on simulator or a real android machine. No errors are displayed and it looks like the edit has gone through but there is no change to the contact record. Can anyone tell me whats wrong with this code please. FWIW I copied and pasted this code from one 'solution/answer' but I dont quite understand why teh selection arguments (line 16) have to have mimetype. Surely as this process build an sqlite query/update string, it is sufficient that rawdataId = "whatever"

public class SaveContact
    {
        public void SaveContactToPhoneBook(string id,string fname, string sname, string Email)
        {
		#if ANDROID

            ContentProviderOperation.Builder builder =ContentProviderOperation.NewUpdate(ContactsContract.Data.ContentUri);
            
            List<ContentProviderOperation> ops = new List<ContentProviderOperation>();
            builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.FamilyName,sname);
            ops.Add(builder.Build());

           
            // now try to save a new Email
           
            String emailSelection = ContactsContract.Data.InterfaceConsts.Id + " = ? "  +ContactsContract.Data.InterfaceConsts.Mimetype + " = ? ";
            String[] emailSelectionArgs = {id,ContactsContract.CommonDataKinds.Email.ContentItemType};
			
			// i am  not sure how the above parameters work - they are obviously passed in builder.WithSelection()  and I assume the Args are substitued for the '?'
			// to construct the sqlite statement which appears to be constructed deep in the code somewher but how?

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

            // Update the contact
             ContentProviderResult[] res;
            try
            {
                res =  Android.App.Application.Context.ContentResolver.ApplyBatch(ContactsContract.Authority, ops);
                ops.Clear();//Add this line  
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine( e.Message);
            }
                      
		#elif IOS
		#endif
        }
    }

There is supposed to be another way (in android) which seems simpler but this doesnt work either but as I say no errors are trapped

     try{
    var contentvalues = new ContentValues();
         var where = ContactsContract.Contacts.InterfaceConsts.Id + "=?" ;
         var whereArgs = new String[] { id };
         contentvalues.Put(ContactsContract.CommonDataKinds.Email.Address, Email);
         // Uri  uri = Uri.Parse("content://com.android.contacts/data");
         var uri2 =  Android.Net.Uri.Parse( "content://com.android.contacts/data");

           Context context = Android.App.Application.Context;

          //var uri = ContactsContract.Contacts.ContentUri;
         context.ContentResolver.Update( uri2, contentvalues, where, whereArgs);
}    catch(Exception e) {var ee = e.Message;}   

Ive been trying to find out whats going wrong for 4 days now - desperately need some kind help

cheers John

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,499 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.