How to access agenda on android and select contact

Asked

Viewed 2,711 times

2

I want to make my application access the system agenda and bring a contact to save the data in database (Sqlite).

  • 1

    Welcome to Sopt Paulo. It would be nice if you showed us what you have already done, so the community can help you better.

  • Paul, in general are used Intents for this, came to try something like this?

  • I already use this link to access the schedule: Uri Uri = Uri.parse("content://com.android.Contacts/Contacts/"); Intent i = new Intent(Intent.ACTION_PICK, Uri); startActivity(i);

  • agr need to return contact data selected

1 answer

3

You’re doing it right, you need to use an Intent to access the application Contact Book android. What remains to be done is to access the contacts in Agenda and return them.

First, you must have a class of MeuContato which will be where you will "save" the contact returned from the agenda. Suppose you want to pick up all the contacts in the agenda with their respective names. In this case, you could use the following code:

public ArrayList<MeuContato> pegarContatos(){

    ArrayList<MeuContato> listaDeContatos = new ArrayList<>();

    Uri agenda = ContactsContract.Contacts.CONTENT_URI;
    Cursor cursor = getContenResolver.query(agenda,null,null,null);

    while(cursor.moveToNext()){

        String nome = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

        /* Pegue mais informações de acordo com os atributos da sua classe MeuContato */

        MeuContato contato = new MeuContato(nome);

        listaDeContatos.add(contato);
    }

    cursor.close();

    return listaDeContatos;
}

Then, just go through the returned contact list and go saving each contact in your database.

OBS.: The second parameter of the method getContenResolver.query() can be replaced by a query, for example, if you want to return only contacts that have an associated phone number, replace the second parameter of the method by ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1"

For more information: Contacs Contract

Browser other questions tagged

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