Tuesday, March 26, 2024
HomeiOS Developmentandroid - enhancing and saving contact particulars utilizing Maui

android – enhancing and saving contact particulars utilizing Maui


earlier thread about saving edited contact particulars

I used to be following this submit to attempt to perceive the right way to save an edited contact – My mission is Maui utilizing VS2022 and targets ios and android although I’m initially making an attempt to get the android app working.
Maui permits a easy means of choosing a contact viz var contact = await Microsoft.Maui.ApplicationModel.Communication.Contacts.Default.PickContactAsync();

It used to have the ability to save utilizing Maui.necessities however that has disappeared and one has to delve into the black magic of android code. (Except somebody can inform me there’s a easy Maui method of saving and edited contact document)

I’ve cannibalised the code prompt within the above submit however being unfamiliar with low degree android code I’m having issue understanding what’s going on
I significantly wish to edit title, tackle (bodily) and e mail. I can appear to edit single fields equivalent to given title however after I use related code to edit or add one other e mail it appears to place the content material within the title area
I assume Ive not formatted it proper – Ive tried studying the Android builders documentation however Im afraid I nonetheless dont get it. Can some form soul inform me the place I am going unsuitable with this code –
Am I understanding the idea proper ? builder is handed the id and mimetype with builder.withselection? To date so good – I can edit and alter easy fields like givenname, However There will be many emails for one contact how does one edit the static fields equivalent to given title but add a brand new e mail? or how does on reference a specific emailaddress inside a selected contact Id?

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

        ContentProviderOperation.Builder builder =ContentProviderOperation.NewUpdate(ContactsContract.Information.ContentUri);
        
        Checklist<ContentProviderOperation> ops = new Checklist<ContentProviderOperation>();
        builder.WithValue(ContactsContract.CommonDataKinds.StructuredName.FamilyName,sname);
        ops.Add(builder.Construct());

       
        // now attempt to save a brand new Electronic mail
       
        String emailSelection = ContactsContract.Information.InterfaceConsts.Id + " = ? "  +ContactsContract.Information.InterfaceConsts.Mimetype + " = ? ";
        String[] emailSelectionArgs = {id,ContactsContract.CommonDataKinds.Electronic mail.ContentItemType};
        
        // i'm  undecided how the above parameters work - they're clearly handed in builder.WithSelection()  and I assume the Args are substitued for the '?'
        // to assemble the sqlite assertion which seems to be constructed deep within the code somewher however how?

        builder = ContentProviderOperation.NewUpdate(ContactsContract.Information.ContentUri);
        builder.WithSelection(emailSelection, emailSelectionArgs);            
        builder.WithValue(ContactsContract.CommonDataKinds.Electronic mail.Tackle, Electronic mail);
     
          ops.Add(builder.Construct());

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



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments