1. 程式人生 > >android 獲取系統通話記錄

android 獲取系統通話記錄

/**
     * 利用系統CallLog獲取通話歷史記錄
     * @param activity
     * @param num  要讀取記錄的數量
     * @return
     */
    public void getCallHistoryList(Activity activity, int num) {
        Cursor cs;
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_CALL_LOG)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity,
                    new String[]{Manifest.permission.READ_CALL_LOG}, 1000);

        }
        cs = activity.getContentResolver().query(CallLog.Calls.CONTENT_URI, //系統方式獲取通訊錄儲存地址
                new String[]{
                        CallLog.Calls.CACHED_NAME,  //姓名
                        CallLog.Calls.NUMBER,    //號碼
                        CallLog.Calls.TYPE,  //呼入/撥出(2)/未接
                        CallLog.Calls.DATE,  //撥打時間
                        CallLog.Calls.DURATION,   //通話時長
                }, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
        int i = 0;
        if (cs != null && cs.getCount() > 0) {
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date_today = simpleDateFormat.format(date);
            for (cs.moveToFirst(); (!cs.isAfterLast()) && i < num; cs.moveToNext(), i++) {
                String callName = cs.getString(0);  //名稱
                String callNumber = cs.getString(1);  //號碼
                //如果名字為空,在通訊錄查詢一次有沒有對應聯絡人
                if (callName == null || callName.equals("")){
                    String[] cols = {ContactsContract.PhoneLookup.DISPLAY_NAME};
                    //設定查詢條件
                    String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + "='"+callNumber+"'";
                    Cursor cursor = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            cols, selection, null, null);
                    int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
                    if (cursor.getCount()>0){
                        cursor.moveToFirst();
                        callName = cursor.getString(nameFieldColumnIndex);
                    }
                    cursor.close();
                }
                //通話型別
                int callType = Integer.parseInt(cs.getString(2));
                String callTypeStr = "";
                switch (callType) {
                    case CallLog.Calls.INCOMING_TYPE:
                        callTypeStr = CallLogInfo.CALLIN;
                        break;
                    case CallLog.Calls.OUTGOING_TYPE:
                        callTypeStr = CallLogInfo.CALLOUT;
                        break;
                    case CallLog.Calls.MISSED_TYPE:
                        callTypeStr = CallLogInfo.CAllMISS;
                        break;
                    default:
                        //其他型別的,例如新增號碼等記錄不算進通話記錄裡,直接跳過
                        Log.i("ssss",""+callType);
                        i--;
                        continue;
                }
                //撥打時間
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date callDate = new Date(Long.parseLong(cs.getString(3)));
                String callDateStr = sdf.format(callDate);
                if (callDateStr.equals(date_today)) { //判斷是否為今天
                    sdf = new SimpleDateFormat("HH:mm");
                    callDateStr = sdf.format(callDate);
                } else if (date_today.contains(callDateStr.substring(0, 7))) { //判斷是否為當月
                    sdf = new SimpleDateFormat("dd");
                    int callDay = Integer.valueOf(sdf.format(callDate));

                    int day = Integer.valueOf(sdf.format(date));
                    if (day - callDay == 1) {
                        callDateStr = "昨天";
                    } else {
                        sdf = new SimpleDateFormat("MM-dd");
                        callDateStr = sdf.format(callDate);
                    }
                } else if (date_today.contains(callDateStr.substring(0, 4))) { //判斷是否為當年
                    sdf = new SimpleDateFormat("MM-dd");
                    callDateStr = sdf.format(callDate);
                }

                //通話時長
                int callDuration = Integer.parseInt(cs.getString(4));
                int min = callDuration / 60;
                int sec = callDuration % 60;
                String callDurationStr = "";
                if (sec > 0) {
                    if (min > 0) {
                        callDurationStr = min + "分" + sec + "秒";
                    } else {
                        callDurationStr = sec + "秒";
                    }
                }

                /**
                 * callName 名字
                 * callNumber 號碼
                 * callTypeStr 通話型別
                 * callDateStr 通話日期
                 * callDurationStr 通話時長
                 * 請在此處執行相關UI或儲存操作,之後會查詢下一條通話記錄
                 */
                Log.i("Msg","callName"+callName);
                Log.i("Msg","callNumber"+callNumber);
                Log.i("Msg","callTypeStr"+callTypeStr);
                Log.i("Msg","callDateStr"+callDateStr);
                Log.i("Msg","callDurationStr"+callDurationStr);
            }
        }
    }