Android Application With Phone Book Synchronization?
I am creating one android test application , in which i have one button. On button click ,i want to synchronize phonebook records with my local database.If record in phone book is
Solution 1:
For getting contact list from your phone book you need write permission in AndroidManifest.XML
(i.e. android.permission.READ_CONTACTS
) .And you can collect contact list using following method.
ShowContact()
{
ArrayList<String> nameList;
ArrayList<String> phoneNoList;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//Query phone here. Covered nextCursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
newString[]{id}, null);
while (pCur.moveToNext()) {
// Do something with phonesString phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
nameList.add(name); // Here you can list of contact.
phoneNoList.add(phoneNo); // And here you can get list of phone number.You have to query separately for getting phone_no,email,name etc // Here you have to iterate this(i.e. nameList) with your list in the database.And your rest of logic.
}
pCur.close();
}
}
}
}
And Let me know if are having any issue in getting contact list.
Solution 2:
public static List fetchContacts(Context context) { final String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY;
finalStringFILTER= DISPLAY_NAME + " NOT LIKE '%@%'";
finalStringORDER= String.format("%1$s COLLATE NOCASE", DISPLAY_NAME);
final String[] PROJECTION = {
ContactsContract.Contacts._ID,
DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER
};
ArrayList<MyContact> contacts = newArrayList<>();
try {
ContentResolvercr= context.getContentResolver();
Cursorcursor= cr.query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, FILTER, null, ORDER);
if (cursor != null && cursor.moveToFirst()) {
do {
MyContactcontact=newMyContact();
// get the contact's informationStringid= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
contact.setContactId(id);
Stringname= cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
contact.setUserName(name);
IntegerhasPhone= cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
// get the user's email addressStringemail=null;
Cursorce= cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, newString[]{ContactsContract.CommonDataKinds.Email.DATA},
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", newString[]{id}, null);
if (ce != null && ce.moveToFirst()) {
email = ce.getString(ce.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
ce.close();
}
contact.setEmailAddress(email);
// get the user's phone numberStringphone=null;
if (hasPhone > 0) {
Cursorcp= cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, newString[]{ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.CONTACT_LAST_UPDATED_TIMESTAMP,
ContactsContract.CommonDataKinds.Phone.PHOTO_URI},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", newString[]{id}, null);
if (cp != null && cp.moveToFirst()) {
phone = cp.getString(cp.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
StringtimeStamp= cp.getString(cp.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_LAST_UPDATED_TIMESTAMP));
contact.setLastUpdateTimestamp(timeStamp);
StringphotoUri= cp.getString(cp.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
contact.setPhotoUri(photoUri);
cp.close();
}
}
contact.setPhone_number(phone);
contacts.add(contact);
} while (cursor.moveToNext());
// clean up cursor
cursor.close();
Utils.list = contacts;
sendMessage(context);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return contacts;
}
Post a Comment for "Android Application With Phone Book Synchronization?"