我正在创建以这种方式读取呼叫日志的应用程序:
Uri uriCallsURI = Uri.parse("content://call_log/calls");
Cursor cur = mainActivity.getContentResolver().query(uriCallsURI, null, null, null, "_id");
一切都适用于 android 2.3.6,但 android 4.0.4 也将 SMS 显示为通话记录。如何防止这种情况发生?
不要硬编码字符串。尝试编写如下代码:
Cursor cur = null;
try {
cur = getContentResolver().query(CallLog.Calls.CONTENT_URI,
new String[] { BaseColumns._ID, CallLog.Calls.NUMBER },
null, null, "_id");
if (cur != null && cur.moveToFirst()) {
do {
Log.i("call", "id = " + cur.getString(0));
Log.i("call", "number = " + cur.getString(1));
Log.i("call", "..................................");
} while (cur.moveToNext());
}
} finally {
if (cur != null) {
cur.close();
}
}
yushulx,问题出在其他地方。三星在通话记录中有列 logtype。我在三星设备上找到了一个答案, 在查询通话记录时返回短信
我已经修改了您的代码,现在我只能在 android 4.0.4 的三星手机上获得通话记录。
Cursor cur = null;
try {
cur = getContentResolver().query(CallLog.Calls.CONTENT_URI,
new String[] { BaseColumns._ID, CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME,
CallLog.Calls.DURATION, CallLog.Calls.DATE, CallLog.Calls.TYPE, CallLog.Calls.NEW},
"logtype=100 OR logtype=500", null, "_id");
displayLogs(cur);
} catch(SQLiteException e){
cur = getContentResolver().query(CallLog.Calls.CONTENT_URI,
new String[] { BaseColumns._ID, CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME,
CallLog.Calls.DURATION, CallLog.Calls.DATE, CallLog.Calls.TYPE, CallLog.Calls.NEW},
null, null, "_id");
displayLogs(cur);
} finally {
if (cur != null) {
cur.close();
}
}