取得手机通话历史记录
1.最近想研究一下获得手机历史通话记录,写了下面这个方法,通过log打出符合条件的通话记录,包括号码,电话连接时间,通话ID和通话时间:
private Cursor getPhoneTracking(String number) {
//通过查询获得符合条件的集合
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
new String[] { "number", "_id", "date", "type", "duration" },
"number =" + number,//选择符合条件记录,如可以写出null,"_id=" +id
null, CallLog.Calls.DEFAULT_SORT_ORDER);
startManagingCursor(cursor);
if (cursor.moveToFirst()) {
String num = "", _id = "", time = "", duration = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
do {
//通话号码
num = cursor.getString(cursor
.getColumnIndex(CallLog.Calls.NUMBER));
//通话id
_id = cursor
.getString(cursor.getColumnIndex(CallLog.Calls._ID));
//通话具体时间
time = cursor.getString(cursor
.getColumnIndex(CallLog.Calls.DATE));
long date = Long.parseLong(time);
//格式化日期
time = sdf.format(date);
//通话时间
duration = cursor.getString(cursor
.getColumnIndex(CallLog.Calls.DURATION));
//以LOG打出通话记录信息
Log.d("===========number=============", num);
Log.d("===========_id=============", _id);
Log.d("===========time=============", time);
Log.d("===========duration=============", duration);
Log.d("====================================",
"===============================");
} while (cursor.moveToNext());
}
return cursor;
}
2.调用
比如我要选择号码为10086的通话记录,可以这样用(电话中应具有10086的通话记录):
Cursor c = getPhoneTracking("10086");//调用
//在list中显示出来
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.callinfo, c, new String[] { "number", "_id", "date",
"type", "duration" }, new int[] { R.id.TextNumber,
R.id.TextName, R.id.TextDuration, R.id.TextType,
R.id.duration });
setListAdapter(adapter);
取得手机通话历史记录
最新推荐文章于 2025-07-06 15:08:21 发布