android 通話記錄和聯絡人查詢
呼叫記錄有三種類型:
來電: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
</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();
}
}