/** 返回查询的数据以及最新数据,将其装入Map中 */
private Map<String, Long> getCallTimeByWhere(final String where) {
Map<String, Long> result = new HashMap<String, Long>();Long total = 0L;
/** 最后一次通话时长 */
Long latestTimeLen = 0L;
/** 最后一次通话时间 */
Long lastDate = null;
final Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
new String[] {CallLog.Calls.DURATION, CallLog.Calls.DATE }, where, null,
CallLog.Calls.DEFAULT_SORT_ORDER);
startManagingCursor(cursor);
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
Long duration = 0L;
try {
duration = Long.parseLong(cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION)));
Long date = Long.parseLong(cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE)));
if (lastDate == null || lastDate < date) {
lastDate = date;
latestTimeLen = duration;
}
} catch (Exception e) {
duration = 1L;
latestTimeLen = duration;
}
total += duration;
}
Log.i("liyong", "total=" + total + "latestTimeLen=" + latestTimeLen);
result.put("result", total);
result.put("latestTimeLen", latestTimeLen);
return result;
}
/** 删除通话记录中满足where条件的数据 */
public int deleteCallData(String where) {
return getContentResolver().delete(CallLog.Calls.CONTENT_URI, where, null);
}
调用方法:
String where = "type=1 or type=2"; //删除来电和去电
ClassName.deleteCallData(where);
String where = "type=1 or type=2";
Map<String, Long> totleResult = ClassName.getCallTimeByWhere(where);