Android 通讯录开发之通话记录

删除通讯记录

![这个是通讯录数据库contacts的通话记录calls表](https://img-blog.csdn.net/20150504105101656)

其中: 这里是删除某个联系人的所有通话记录。其余需求类似!       
  1. 列表内容
    • 1.number是电话号码
    • 2.date 是日期 long型
    • 3.type
      • 1—代表 打进来的电话
      • 2—代表 打出去的电话
      • 3—代表 未接电话
    • 4.matched-number 是格式化的电话号码
      其存储的电话号码格式可以由android.telephony.PhoneNumberUtils.formatNumber(number);得到
  2. 所需权限
    <!-- 读写通话记录权限 -->
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.WRITE_CALL_LOG"/>

  3. 代码块
    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!!! 研究不够啊!代码乱飞啊! 见谅啊各位大大们!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值