删除通讯记录

其中: 这里是删除某个联系人的所有通话记录。其余需求类似!
- 列表内容
- 1.number是电话号码
- 2.date 是日期 long型
- 3.type
- 1—代表 打进来的电话
- 2—代表 打出去的电话
- 3—代表 未接电话
- 4.matched-number 是格式化的电话号码
其存储的电话号码格式可以由android.telephony.PhoneNumberUtils.formatNumber(number);得到
所需权限
<!-- 读写通话记录权限 -->
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>代码块
ContentResolver localContentResolver = context.getContentResolver();
Uri uri = CallLog.Calls.CONTENT_URI;
Cursor cursor=localContentResolver.query(uri, new String[]{"_id"}, "number=?", new String[] { number }, null);
while(cursor.moveToNext()) {
int id = cursor.getInt(0);
localContentResolver.delete(uri, "_id=?", new String[] {id + ""});
}
cursor.close()
加载通讯记录
1. 代码
这里采用了一个系统提供的异步类来加载数据 AsyncQueryHandler 此类方便用户异步操作 ContentResolver
`ContentResolver cr = context.getContentResolver();
myAsyncQuery asyncquery = new myAsyncQuery(cr);
asyncquery.startQuery(0, null, CallLog.Calls.CONTENT_URI,
new String[] { CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME,
CallLog.Calls.TYPE, CallLog.Calls.DATE,
CallLog.Calls.DURATION, }, null, null,
CallLog.Calls.DEFAULT_SORT_ORDER
); `
下面看下
myAsyncQuery 这个类
其主要方法是onQueryComplete()方法。用法很明了参数很清晰。不废话 上代码:
`
public static class myAsyncQuery extends AsyncQueryHandler
private HistoryAdapter mAdapter;
public myAsyncQuery(ContentResolver cr) {
super(cr);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
// 实例化一个数组链表
ArrayList<CallHistoryModel> contactslist = new ArrayList<CallHistoryModel>();
if (cursor != null && cursor.getCount() > 0) {
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
CallHistoryModel model = new CallHistoryModel();// 建立一个模型类
model.setPhonenum(cursor.getString(cursor
.getColumnIndex(CallLog.Calls.NUMBER)));
model.setName(cursor.getString(cursor
.getColumnIndex(CallLog.Calls.CACHED_NAME)));
model.setType(cursor.getInt(cursor
.getColumnIndex(CallLog.Calls.TYPE)));
model.setData(cursor.getLong(cursor
.getColumnIndex(CallLog.Calls.DATE)));
model.setDuration(cursor.getLong(cursor
.getColumnIndex(CallLog.Calls.DURATION)));
contactlist.add(model);}
手机原身自带的某个号码通话记录多少次!!我测试了一下。 手机获取通话记录一般获取500条! 那么里面多少重复的就显示在一起咯!! 次数就知道咯!! 我这样获取次数和系统自带的通话记录一样
这个编辑器太难用了!!! 什么markdown!!! 研究不够啊!代码乱飞啊! 见谅啊各位大大们!!
本文介绍如何在Android系统中删除指定联系人的通话记录,包括数据库表结构、权限设置及使用ContentResolver进行增删查改的方法。此外,还提供了一种通过AsyncQueryHandler异步加载大量通话记录的有效方式。
1万+

被折叠的 条评论
为什么被折叠?



