我正在创建一个android测试应用程序,其中有一个按钮。单击按钮,我想将电话簿记录与本地数据库同步。如果电话簿中的记录不在数据库表中,则插入它,否则保持原样。那么我该怎么做呢?
要从通讯录中获取联系人列表,您需要在AndroidManifest.XML
(即android.permission.READ_CONTACTS
)中拥有写入权限。您可以使用以下方法收集联系人列表。
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 next
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
// Do something with phones
String 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();
}
}
}
}
如果您在获取联系人列表时遇到任何问题,请告诉我。
公共静态列表fetchContacts(上下文上下文){final字符串DISPLAY_NAME=ContactsContract.Contacts.DISPLAY_NAME _PRIMARY;
final String FILTER = DISPLAY_NAME + " NOT LIKE '%@%'";
final String ORDER = String.format("%1$s COLLATE NOCASE", DISPLAY_NAME);
final String[] PROJECTION = {
ContactsContract.Contacts._ID,
DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER
};
ArrayList<MyContact> contacts = new ArrayList<>();
try {
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, FILTER, null, ORDER);
if (cursor != null && cursor.moveToFirst()) {
do {
MyContact contact = new MyContact();
// get the contact's information
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
contact.setContactId(id);
String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
contact.setUserName(name);
Integer hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
// get the user's email address
String email = null;
Cursor ce = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Email.DATA},
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{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 number
String phone = null;
if (hasPhone > 0) {
Cursor cp = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.CONTACT_LAST_UPDATED_TIMESTAMP,
ContactsContract.CommonDataKinds.Phone.PHOTO_URI},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
if (cp != null && cp.moveToFirst()) {
phone = cp.getString(cp.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String timeStamp = cp.getString(cp.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_LAST_UPDATED_TIMESTAMP));
contact.setLastUpdateTimestamp(timeStamp);
String photoUri = 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;
}