Question Detail
Fetching more than 1000 contacts from android device?
I am trying to get phoneBook contact in my application, but when i getting from the Mobile my app going hand for some min, How to resolve this issue?
Thread Reply
alex levine
- 1 years ago
You can use below mentioned function to get PhoneBook contact in your Android Application.
int contact_id;
String contact_name = "", photo_uri = "", contact_data = "";
String email = "", phoneExt = "", phone = "";public void initeContacts() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
uri = ContactsContract.CommonDataKinds.Contactables.CONTENT_URI;
} else {
uri = ContactsContract.Data.CONTENT_URI;
}
Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, contactSorting);
int oldid = 0;
String oldName = "", oldContact_data = "";
final int mimeTypeIdx = cursor.getColumnIndex(ContactsContract.Data.MIMETYPE);
final int idIdx = cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID);
final int nameIdx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int dataIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.DATA);
final int photo = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.PHOTO_URI);
final int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Contactables.TYPE);
final int account_type = cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE);
contactList.add(new Contact(Pref.getString(mActivity, USERID_PREF), Pref.getString(mActivity, USERNAME_PREF), Pref.getString(mActivity, USERPIC_PREF), "", Pref.getString(mActivity, USERPhone_PREF), Pref.getString(mActivity, USEREMAIL_PREF), Pref.getString(mActivity, USERID_PREF), true));
while (cursor.moveToNext()) {
contact_id = cursor.getInt(idIdx);
photo_uri = cursor.getString(photo);
contact_name = cursor.getString(nameIdx);
String contact_acc_type = cursor.getString(account_type);
int contact_type = cursor.getInt(typeIdx);
contact_data = cursor.getString(dataIdx);
if (contact_id != oldid || !contact_name.equals(oldName) || !replaceMethod(contact_data).equals(replaceMethod(oldContact_data))) {
oldName = contact_name;
oldid = contact_id;
oldContact_data = contact_data;
if (photo_uri == null) {
photo_uri = "";
}
String[] KEY = new String[]{"URL", "email", "phone"};if (isValidEmailID(mActivity, contact_data)) {
email = contact_data;
} else {
String[] pho = splitPhone(contact_data);
phoneExt = pho[0];
phone = pho[1];
}
String[] VALUE = new String[]{GETMEMBERINFO_URL, email, phone};
new APICLASS(mActivity).RequestAPICall(KEY, VALUE, new APIResponce() {
@Override
public void onAPIResponse(String str) {
try {
JSONObject contactJson = new JSONObject(str);
if (contactJson.getBoolean("success")) {
JSONObject messgJson = contactJson.getJSONObject("message");
int id = Integer.parseInt(messgJson.getString("id"));
String image = messgJson.getString("pic");
String name = messgJson.getString("name");
contactList.add(new Contact(id + "", name, image, phoneExt, phone, email, contact_id + "", true));
}
} catch (Exception e) {
e.printStackTrace();
}
}@Override
public void onAPIError(String str) {
contactList.add(new Contact(contact_id + "", contact_name, photo_uri, phoneExt, phone, email, contact_id + "", true));
}
});Log.d("READNGConTACT", contact_id + "___" + photo_uri + "___" + contact_name + "___" + contact_acc_type + "___" + contact_type + "___" + contact_data);
}
}
cursor.close();
}
Goal Ploy - Money Management App