patternjavaMinor
Reading complete contact information in Android
Viewed 0 times
contactreadingandroidcompleteinformation
Problem
My goal is to read complete contact information on an Android application. This is very slow and my understanding is that since the contacts are stored locally in a phone database, it shouldn't take long to fetch complete contact details.
```
public void readContacts() {
Log.d("TAG", " -> Into readContacts");
// Get the ContentResolver
ContentResolver cr = getContentResolver();
// Get the Cursor of all the contacts
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// Move the cursor to first. Also check whether the cursor is empty or not.
if (cursor.moveToFirst()) {
// Iterate through the cursor
do {
// Get the contacts name
Contact contactToSave = new Contact();
contactToSave.setIsUploaded(false);
String id = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
Log.d(TAG, " ID " + id);
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactToSave.setFirstName(name);
Log.d(TAG, " Complete Name " + name);
String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] orgWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI, null, orgWhere, orgWhereParams, null);
if (orgCur.moveToFirst()) {
String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
contactToSave.setCompany(orgName);
}
orgCur.close();
Cursor emailCur = cr.query(
```
public void readContacts() {
Log.d("TAG", " -> Into readContacts");
// Get the ContentResolver
ContentResolver cr = getContentResolver();
// Get the Cursor of all the contacts
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// Move the cursor to first. Also check whether the cursor is empty or not.
if (cursor.moveToFirst()) {
// Iterate through the cursor
do {
// Get the contacts name
Contact contactToSave = new Contact();
contactToSave.setIsUploaded(false);
String id = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
Log.d(TAG, " ID " + id);
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactToSave.setFirstName(name);
Log.d(TAG, " Complete Name " + name);
String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] orgWhereParams = new String[]{id,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI, null, orgWhere, orgWhereParams, null);
if (orgCur.moveToFirst()) {
String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
contactToSave.setCompany(orgName);
}
orgCur.close();
Cursor emailCur = cr.query(
Solution
Use AsyncQueryHandler, a helper class to help make handling asynchronous
It basically wraps the
Just use to call the
Example
Use below method to fetch contact and add projection, selection according to your requirment.
ContentResolver queries easier.It basically wraps the
ContentResolver object and handles background execution of its operations (CRUD) as well as passing messages (result) from the between threads (background and main/UI).Just use to call the
queryHandler.startQuery method and you will get callback in onQueryComplete method.Example
class QueryHandler extends AsyncQueryHandler {
public QueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
switch (token) {
case CONTACT_QUERY:
//Do your stuff here
if (cursor.moveToFirst()) {
// Iterate through the cursor
do {
String id = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
Log.e(TAG, " ID " + id);
} while (cursor.moveToNext());
}
break;
}
}Use below method to fetch contact and add projection, selection according to your requirment.
public void fetchContact(){
queryHandler.startQuery(CONTACT_QUERY, null, ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
}Code Snippets
class QueryHandler extends AsyncQueryHandler {
public QueryHandler(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
switch (token) {
case CONTACT_QUERY:
//Do your stuff here
if (cursor.moveToFirst()) {
// Iterate through the cursor
do {
String id = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
Log.e(TAG, " ID " + id);
} while (cursor.moveToNext());
}
break;
}
}public void fetchContact(){
queryHandler.startQuery(CONTACT_QUERY, null, ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
}Context
StackExchange Code Review Q#148415, answer score: 5
Revisions (0)
No revisions yet.