我正在创建以这种方式读取呼叫记录的应用程序:
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显示为通话记录。 如何防止这种情况发生?
答案 0 :(得分:0)
不要硬编码字符串。尝试编写如下代码:
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();
}
}
答案 1 :(得分:0)
我修改了你的代码,现在我只获得了使用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();
}
}