安卓工具类分享-查询通话记录

查询用户通话记录的工具类分享给大家

package com.warmdoctor.service.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.provider.CallLog.Calls;

/**
 *  Copyright   2016    CoderDream's Eclipse
 * 
 *  All right reserved.
 *  
 *  Created on 2016年8月15日 下午5:02:45
 *  
 *  Update on 2016年8月15日 下午5:02:45
 * 
 *  @author xiaoming
 *  
 *  @mail sgyingyin@sina.com
 * 
 *  @tags An overview of this file: 查询用户通话记录
 * 
 */
public class CallUtil {

    // 请在Manifest文件中声明权限 <uses-permission android:name="android.permission.READ_CALL_LOG" />

    private static CallUtil callUtil;
    private Context mContext;

    private CallUtil(Context context) {
        mContext = context;
    }

    public synchronized static CallUtil getInstance(Context context) {
        if (callUtil == null)
            callUtil = new CallUtil(context);
        return callUtil;
    }

    /**
     * @author xiaoming 2016年8月15日
     * @describe    获取所有的通话记录
     * @return
     * @returnType List<CallsLog>
     */
    public List<CallsLog> getCallLog() {
        if(mContext == null)
            return null;
        List<CallsLog> logs = new ArrayList<CallsLog>();
        Cursor cursor = mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                // 号码
                String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
                // 呼叫类型
                int type = Integer.parseInt(cursor.getString(cursor.getColumnIndex(Calls.TYPE)));
                // 呼叫时间
                long date = Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(Calls.DATE)));
                // 联系人
                String cached_name = cursor.getString(cursor.getColumnIndexOrThrow(Calls.CACHED_NAME));
                // 通话时间,单位:s
                long duration = Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(Calls.DURATION)));

                CallsLog calls = new CallsLog(number, type, date, cached_name, duration);
                logs.add(calls);

            } while (cursor.moveToNext());

        }
        return logs;
    }

    /**
     * @author xiaoming 2016年8月15日
     * @describe    查询某人的通话记录
     * @param numberStr 电话号码
     * @return
     * @returnType List<CallsLog>
     */
    public List<CallsLog> getCallLogOfNumber(String numberStr){
        if(mContext == null)
            return null;
        List<CallsLog> logs = new ArrayList<CallsLog>();
        String selection = "number=" + numberStr;
        Cursor cursor = mContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, selection, null, null);
        if (cursor.moveToFirst()) {
            do {
                // 号码
                String number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
                // 呼叫类型
                int type = Integer.parseInt(cursor.getString(cursor.getColumnIndex(Calls.TYPE)));
                // 呼叫时间
                long date = Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(Calls.DATE)));
                // 联系人
                String cached_name = cursor.getString(cursor.getColumnIndexOrThrow(Calls.CACHED_NAME));
                // 通话时间,单位:s
                long duration = Long.parseLong(cursor.getString(cursor.getColumnIndexOrThrow(Calls.DURATION)));

                CallsLog calls = new CallsLog(number, type, date, cached_name, duration);
                logs.add(calls);

            } while (cursor.moveToNext());

        }
        return logs;
    }

    public class CallsLog {

        /**
         * 号码
         */
        private String number;
        /**
         * 呼叫类型 Calls.INCOMING_TYPE呼入 Calls.OUTGOING_TYPE呼出 Calls.MISSED_TYPE未接
         * Calls.MISSED_TYPE语音信箱
         */
        private int type;
        /**
         * 通话日期
         */
        private long date;
        /**
         * 联系人
         */
        private String cached_name;
        /**
         * 通话时长
         */
        private long duration;

        public CallsLog(String number, int type, long date, String cached_name, long duration) {
            super();
            this.number = number;
            this.type = type;
            this.date = date;
            this.cached_name = cached_name;
            this.duration = duration;
        }

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            this.number = number;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public long getDate() {
            return date;
        }

        public void setDate(long date) {
            this.date = date;
        }

        public String getCached_name() {
            return cached_name;
        }

        public void setCached_name(String cached_name) {
            this.cached_name = cached_name;
        }

        public long getDuration() {
            return duration;
        }

        public void setDuration(long duration) {
            this.duration = duration;
        }

        @Override
        public String toString() {
            return "CallsLog [number=" + number + ", type=" + type + ", date=" + date + ", cached_name=" + cached_name + ", duration=" + duration
                    + "]";
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值