Store contact information on android Xamarin

Asked

Viewed 115 times

1

void button_Click(object sender, EventArgs e)
    {
        //Create a new intent for choosing a contact
        var contactPickerIntent = new Intent(Intent.ActionPick,
            Android.Provider.ContactsContract.CommonDataKinds.Email.ContentUri);
        //Start the contact picker expecting a result
        // with the resultCode '101'
        StartActivityForResult(contactPickerIntent,101);   // mostra a lista dos contactos do telefone, so com as pessoas com email.
    }

my code is this, when I click the button appear in all contacts of the user with email. And what I wanted was to know how I keep this information, for example the user selects a person from his contacts and I want to email that person, but for that I need to keep the contact information and that’s what I don’t know how to do.

Thanks to those who can help.

1 answer

0


Overwrite the method OnActivityResult, where on the date it will return the information of the selected contact as in the following example:

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);

    var cursor = ContentResolver.Query(data.Data, null, null, null, null);
    cursor.MoveToFirst();

    var columnNumber = cursor.GetColumnIndex(Phone.Number);
    var phoneNumber = cursor.GetString(columnNumber);

    var columnName = cur.GetColumnIndex(ContactsContract.ContactNameColumns.DisplayNamePrimary);
    var name = cursor.GetString(columnName);
}
  • It worked, thank you very much.

  • In this case you are just taking the name and contact number, but you have other columns, being possible you get the return of all of them in the method GetColumnNames() that will return an array of strings with all possible column names

  • but it will also depend on what I put in mine contactPickerIntent , in my example only returns the email and the name, the number is 0.

  • Exactly, if you want to take the number, the call should be made as follows: var contactPickerIntent = new Intent(Intent.ActionPick,
 Android.Provider.ContactsContract.CommonDataKinds.Phone.ContentUri);
 //Start the contact picker expecting a result
 // with the resultCode '101'
 StartActivityForResult(contactPickerIntent, 101);

  • but if I want to take the email and the number as I do?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.