查詢通話記錄
private static final String[] CALLLOGS_PROJECTION = new String[]{CallLog.Calls._ID,
CallLog.Calls.CACHED_NAME, CallLog.Calls.NUMBER, CallLog.Calls.TYPE, CallLog.Calls.DATE,
CallLog.Calls.DURATION};
/**
*
* 概述:獲取最近10條通話記錄
*/
public ArrayList getCallLogs() {
mCallLogBeans.clear();
ContentResolver resolver = mContext.getContentResolver();
// 獲取手機聯系人
@SuppressLint("MissingPermission")
Cursor phoneCursor = resolver.query(CallLog.Calls.CONTENT_URI,
CALLLOGS_PROJECTION, null, null, "date DESC");
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
// 得到手機號碼
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
// 當手機號碼為空的或者為空字段 跳過當前循環
if (TextUtils.isEmpty(phoneNumber))
continue;
// 得到聯系人名稱,通過數據庫查詢的方式有的手機有問題,通話記錄不會緩存聯系人姓名(中興天機7),只能去查通訊錄對應的人名
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
if (TextUtils.isEmpty(contactName)) {
contactName = getPhoneTrueName(phoneNumber);
}
if (TextUtils.isEmpty(contactName)) continue;
PersonBean bean = new PersonBean();
bean._id = phoneCursor.getLong(phoneCursor.getColumnIndex(CallLog.Calls._ID));
bean.name = contactName;
bean.phoneNum = phoneNumber.replace(" ", "").replace("+86", "");
bean.date = phoneCursor.getLong(phoneCursor.getColumnIndex(CallLog.Calls.DATE));
bean.duration = phoneCursor.getString(phoneCursor.getColumnIndex(CallLog.Calls.DURATION));
bean.type = phoneCursor.getInt(phoneCursor.getColumnIndex(CallLog.Calls.TYPE));
bean.pinyinName = Pinyin.toPinyin(bean.name, "");
LogUtils.instance().d(TAG, "PersonBean [" +bean.toString()+"]");
boolean isContained = false;
for (PersonBean bean2 : mCallLogBeans) {
if (bean2.phoneNum.equals(bean.phoneNum)) {
isContained = true;
break;
}
}
if (!isContained) {//如果沒有這個號碼則加入數組
mCallLogBeans.add(bean);
if (mCallLogBeans.size() > 8) {
break;
}
}
}
phoneCursor.close();
}
return mCallLogBeans;
}
秒數轉時長
public static long[] secondNum2Time(String timeStr) {
long[] longs = new long[]{0,0,0};
if (TextUtils.isEmpty(timeStr)) return longs;
long time = Long.parseLong(timeStr);
long hour = time / 3600;
long minute = time / 60 % 60;
long second = time % 60;
longs[0] = hour;
longs[1] = minute;
longs[2] = second;
return longs;
}
通話記錄的type
public String getTypeStr() {
if (CallLog.Calls.INCOMING_TYPE == type) {
return "來電";
} else if (CallLog.Calls.OUTGOING_TYPE == type) {
return "去電";
}else if (CallLog.Calls.MISSED_TYPE == type) {
return "未接";
}else if (CallLog.Calls.VOICEMAIL_TYPE == type) {
return "語音郵件";
}else if (CallLog.Calls.REJECTED_TYPE == type) {
return "拒絕";
}else if (CallLog.Calls.BLOCKED_TYPE == type) {
return "阻止";
} else {
return "未知";
}
}