轉自http://blog.csdn.net/davayunhuijia/article/details/7778240
通訊記錄有三種類型:
來電:CallLog.Calls.INCOMING_TYPE (常量值:1)
已撥:CallLog.Calls.OUTGOING_TYPE(常量值:2)
未接:CallLog.Calls.MISSED_TYPE(常量值:3)
查看源碼中的聲明:
<provider android:name="CallLogProvider"android:authorities="call_log"android:syncable="false"android:multiprocess="false"android:readPermission="android.permission.READ_CONTACTS"android:writePermission="android.permission.WRITE_CONTACTS">
</provider>
需要聲明的許可權
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
系統的通話記錄,是通過 ContentProvider 來對外共用的 Uri CallLog.Calls.CONTENT_URI :
等價於:Uri.parse("content://call_log/calls");
查詢出所有記錄
ContentResolver resolver = getContentResolver(); resolver.query(CallLog.Calls.CONTENT_URI, null, null, new String[]{"15101689022"}, null);
查詢某一個連絡人的所有記錄(按電話號碼)
resolver.query(CallLog.Calls.CONTENT_URI, null, "number=?", new String[]{"15101689022"}, null);
查詢某一個連絡人的所有未接電話記錄(按電話號碼) resolver.query(CallLog.Calls.CONTENT_URI, String[]{"15101689022"}, null);
刪除某連絡人最近的一次來電 ContentResolver resolver = getContentResolver();
/* 這裡涉及到內容提供者的知識,其實這裡是直接在操作
Android 的資料庫,十分痛苦 */
Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, new String[]{"_id"}, "number=? and (type=1 or type=3)", new String[]{"15101689022"}, "_id desc limit 1"); null, "number=? and type=3", new if(cursor.moveToFirst()) { int id = cursor.getInt(0); resolver.delete(CallLog.Calls.CONTENT_URI,
"_id=?", new String[] {id + ""}); }
下面說說代碼是怎麼用的
先說說 Phone.CONTENT_URI,擷取連絡人的時候需要去這個url中去找資料 。它所指向的其實是“content:// com.android.contacts/data/phones”。這個url 對應著contacts表 和 raw_contacts表 以及 data表 所以說我們的資料都是從這三個表中擷取的。
這裡強調一下query第二個參數
private static final String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };
它的意思是只去表中找 顯示名稱 電話號碼 頭像ID 連絡人ID 這4個資料 ,如果你須要其它資料 的話 就須要修改這裡。
獲得手機通訊錄連絡人資訊
/**得到手機通訊錄連絡人資訊**/
private void getPhoneContacts() {
ContentResolver resolver = mContext.getContentResolver();
// 擷取手機連絡人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
//得到手機號碼
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
//當手機號碼為空白的或者為空白欄位 跳過當前迴圈
if (TextUtils.isEmpty(phoneNumber))
continue;
//得到連絡人名稱
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
//得到連絡人ID
Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);
//得到連絡人頭像ID
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
//得到連絡人頭像Bitamp
Bitmap contactPhoto = null;
//photoid 大於0 表示連絡人有頭像 如果沒有給此人設定頭像則給他一個預設的
if(photoid > 0 ) {
Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
}else {
contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);
}
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
mContactsPhonto.add(contactPhoto);
}
phoneCursor.close();
}
}
獲得手機sim卡連絡人資訊
sim卡和手機本人 擷取的方式類似 只是url有點不一樣 ,須要注意的一點是 sim卡 是沒有連絡人頭像的。
/**得到手機SIM卡連絡人人資訊**/
private void getSIMContacts() {
ContentResolver resolver = mContext.getContentResolver();
// 擷取Sims卡連絡人
Uri uri = Uri.parse("content://icc/adn");
Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null,
null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
// 得到手機號碼
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
// 當手機號碼為空白的或者為空白欄位 跳過當前迴圈
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到連絡人名稱
String contactName = phoneCursor
.getString(PHONES_DISPLAY_NAME_INDEX);
//Sim卡中沒有連絡人頭像
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
}
phoneCursor.close();
}
}