安卓工具類分享-查詢通話記錄

查詢用戶通話記錄的工具類分享給大家

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 [email protected]
 * 
 *  @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
                    + "]";
        }

    }

}
發佈了27 篇原創文章 · 獲贊 94 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章