to get email address from the addressbook in android




first add uses_permissions for reading contacts in the manifest file. then add this code inside any function(i.e onCreate() of activity)

Uri mContacts = Contacts.ContactMethods.CONTENT_URI;

String[] projection = new String[] {

Contacts.ContactMethods.PERSON_ID,
Contacts.ContactMethods.DISPLAY_NAME,
Contacts.ContactMethods.KIND,
Contacts.ContactMethods.DATA };
Cursor c = this.managedQuery(mContacts, projection, null, null,
Contacts.ContactMethods.DISPLAY_NAME + " ASC");
c.moveToFirst();
int previousId = -1;

do {
int personId = c.getInt(c
.getColumnIndex(Contacts.ContactMethods.PERSON_ID));
if (personId != previousId) {
String personName = c.getString(c
.getColumnIndex(Contacts.ContactMethods.DISPLAY_NAME));
Log.i("name",personName);
}
int dataKind = c.getInt(c
.getColumnIndex(Contacts.ContactMethods.KIND));
if (dataKind == 1) {
String personEmail = c.getString(c
.getColumnIndex(Contacts.ContactMethods.DATA));
Log.i("email",personEmail);
}
previousId = personId;

} while (c.moveToNext());

// it's output is shown in the log file





Comments

Popular posts from this blog

Run tasks in background in Spring

Conditional field inclusion in Jackson and Spring Boot

How to configure Wildfly 10 to use MySQL