I want to make my application access the system calendar and bring a contact to save the data in bank (SQlite).
I want to make my application access the system calendar and bring a contact to save the data in bank (SQlite).
You're doing it right, you need to use an Intent to access the Android Contacts application. What you have to do is access your Contacts from the Address Book and return them.
First, you must have a MeuContato
class that will be where you will "save" the contact returned from the schedule. Suppose you want to get all contacts from the address book with their 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;
}
Next, you simply scroll through the returned contact list and save each contact in your database.
NOTE: The second parameter of the getContenResolver.query()
method can be replaced with a query, for example, if you want to return only the contacts that have an associated phone number, replace the second parameter of method by ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1"
For more information: Contacs Contract