public class ContactsDao {
private Context context;
// 聯繫人信息名字
private static String display_name;
// // 聯繫人信息ID
private static String contactId;
// 聯繫人信息Uri
private static Uri contentUri;
// 聯繫人號碼Uri字段
private static Uri phoneContentUri;
// 聯繫人的號碼ID字段
private static String contactId2;
// 聯繫人號碼字段
private static String contactNum;
// 聯繫人郵箱的Uri
private static Uri contactEmailUri;
// 聯繫人郵箱字段
private static String contactEmailData;
// 聯繫人郵箱ID字段
private static String contactEmailID; android
static {
contentUri = ContactsContract.Contacts.CONTENT_URI;
phoneContentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
contactEmailUri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
display_name = ContactsContract.Contacts.DISPLAY_NAME;
contactId = ContactsContract.Contacts._ID;
contactId2 = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
contactNum = ContactsContract.CommonDataKinds.Phone.NUMBER;
contactEmailID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
contactEmailData = ContactsContract.CommonDataKinds.Email.DATA;
} this
public ContactsDao(Context context) {
super();
this.context = context;
} get
public Cursor findLocalContacts() {
Cursor cursor = context.getContentResolver().query(contentUri, null, null, null, null);
// 聯繫人名字
String displayName = cursor.getString(cursor.getColumnIndex(display_name));
// 聯繫人ID
String contactsID = cursor.getString(cursor.getColumnIndex(contactId));
// 使用ContentResolver查找聯繫人電話號碼
Cursor phones = context.getContentResolver().query(phoneContentUri, null, contactId2 + "=" + contactsID, null, null); io
// 遍歷查詢結果,獲取聯繫人的多個號碼
while (phones.moveToNext()) {
phones.getString(phones.getColumnIndex(contactNum));
}
phones.close(); class
// 使用ContentResolver查找聯繫人聯繫人郵箱
Cursor emails = context.getContentResolver().query(contactEmailUri, null, contactEmailID + "=" + contactsID, null, null);
while (emails.moveToNext()) {
emails.getString(emails.getColumnIndex(contactEmailData));
}
emails.close();
cursor.close(); email
return cursor;
} 遍歷
private void insetLocalContacts() {
// 建立一個空的ContantValues
ContentValues values = new ContentValues();
// 向RawContacts.CONTENT_URI執行空值插入,獲取系統返回的rawContactId
Uri rawContactUri = context.getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
values.clear(); 權限
// *****************添加聯繫人姓名***************
values.put(Data.RAW_CONTACT_ID, rawContactId);
// 設置內容類型
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
// 設置聯繫人名字
values.put(StructuredName.GIVEN_NAME, "請輸入姓名");
// 向聯繫人URI添加聯繫人姓名
context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
values.clear(); 查詢
// *****************添加聯繫人電話***************
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
// 設置聯繫人的號碼
values.put(Phone.NUMBER, "聯繫人電話");
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
// 向聯繫人URI添加聯繫人電話
context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
values.clear(); static
// *****************添加聯繫人郵箱***************
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
values.put(Email.DATA, "請輸入郵箱");
values.put(Email.TYPE, Email.TYPE_WORK);
context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
} }
<!-- 讀取聯繫人權限 --> <uses-permission android:name="android.permission.READ_CONTACTS" /> <!-- 寫入聯繫人的權限 --> <!-- <uses-permission android:name="android.permission.WRITE_CONTACTS" /> -->