CallLogProvider介绍
系统的通话记录,是通过 ContentProvider 来对外共享的
负责存放呼叫记录的内容提供者源码在 ContactsProvider 项目下:
源码路径:
com/Android/providers/contacts/CallLogProvider.Java
使用到的数据库在:
/data/data/com.android.providers.contacts/databases/contacts2.db 表名:calls
呼叫记录有三种类型:
来电: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_CALL_LOG" />
需要写入通话记录时需要声明权限:
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
通话记录操作
查询所有的记录
/**
* 查询所有的记录
*/
private void queryAll() {
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, null, null, null, null);
Log.d(TAG, "call log count:" + cursor.getCount());
while (cursor.moveToNext()) {
String callNumber = cursor.getString(cursor.getColumnIndex("number"));
Log.d(TAG, "call log:" + callNumber);
}
}
查询已知号码的记录
ContentResolver resolver = getContentResolver();
resolver.query(CallLog.Calls.CONTENT_URI, null, "number=?", new String[]{"15220000000"}, null);
ps: CallLog.Calls.CONTENT_URI : 等价于:Uri.parse(“content://call_log/calls”)
删除已知号码的记录
/**
* 删除某个记录
*/
private void deleteCallLog(String callLog) {
ContentResolver resolver = getContentResolver();
int result = resolver.delete(CallLog.Calls.CONTENT_URI, "number=?", new String[] { callLog });
if (result > 0) {
Log.d(TAG, "deleted success:" + callLog);
} else {
Log.d(TAG, "deleted fail:" + callLog);
}
}
查询某一个联系人的所有未接电话记录(按电话号码)
resolver.query(CallLog.Calls.CONTENT_URI, null, "number=? and type=3", new String[]{"15100000000"}, null);
type取值参照上面呼叫记录的三种类型
删除某联系人最近的一次来电
/**
* 删除某联系人最近的一次来电
*
* @param callLog
*/
private void deletLastCallLog(String callLog) {
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, new String[] { "_id" }, "number=? and (type=1 or type=3)", new String[] { callLog },
"_id desc limit 1");
if (cursor.moveToFirst()) {
int id = cursor.getInt(0);
resolver.delete(CallLog.Calls.CONTENT_URI, "_id=?", new String[] { id + "" });
}
}
最后附上示例代码:http://download.csdn.net/detail/osanyecao1234567/9591196