android 獲取通話記錄

 
 

在manifest添加如下權限
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_CONTACTS" />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); } } }
相關文章
相關標籤/搜索