İsa*_* C. 4 android calllog telephonymanager android-cursor
我有一个可以在下面运行的代码。但我无法限制它。我只想获取最近 20 条通话记录。但这就是我查看所有时间搜索日志的方式。
应该只是最后的通话记录,我只需要看20条就可以了。任何帮助,我将不胜感激。
我的守则;
private void getCallLogs() {
ContentResolver cr = getBaseContext().getContentResolver();
Cursor c = cr.query(CallLog.Calls.CONTENT_URI, null, null, null, null);
int totalCall = 1;
if (c != null) {
totalCall = c.getCount();
if (c.moveToFirst()) {
for (int j = 0; j < totalCall; j++) {
String callDate = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.DATE));
String phNumber = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.NUMBER));
String callDuration = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.DURATION));
Date dateFormat= new Date(Long.valueOf(callDate));
String callDayTimes = String.valueOf(dateFormat);
String direction = null;
switch (Integer.parseInt(c.getString(c.getColumnIndexOrThrow(CallLog.Calls.TYPE)))) {
case Telephony.Sms.MESSAGE_TYPE_INBOX:
direction = "OUTGOING";
break;
case Telephony.Sms.MESSAGE_TYPE_SENT:
direction = "INGOING";
break;
case Telephony.Sms.MESSAGE_TYPE_OUTBOX:
direction = "MISSED";
break;
default:
break;
}
Toast.makeText(this, phNumber + direction + callDuration + callDayTimes, Toast.LENGTH_SHORT).show();
}
}
c.close();
}
}
Run Code Online (Sandbox Code Playgroud)
请将此权限添加到您的清单文件中。
Run Code Online (Sandbox Code Playgroud)<uses-permission android:name="android.permission.READ_CALL_LOG"/>
将此函数添加到您的 Activity 文件中,并在 oncreate 函数中调用它。
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
Run Code Online (Sandbox Code Playgroud)
要调用此函数,请将其添加到 Activity 中的 oncreate 函数中。
getCallLogs();
| 归档时间: |
|
| 查看次数: |
8394 次 |
| 最近记录: |