以编程方式删除所有电话呼叫日志



我正在尝试创建一个方法,一键删除所有调用日志。这是我的代码:

private void deletePhoneCallLogs() {
Cursor cursor = getContext().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
long id = cursor.getLong(cursor.getColumnIndexOrThrow(CallLog.Calls._ID));
Uri deleteUri = ContentUris.withAppendedId(CallLog.Calls.CONTENT_URI, id);
getContext().getContentResolver().delete(deleteUri, null, null);
}
cursor.close();
}

我的权限清单:

<uses-permission android:name="android.permission.WRITE_CALL_LOG"></uses-permission>
<uses-permission android:name="android.permission.READ_CALL_LOG"></uses-permission>

我有一个我不明白的错误,就是这个:

java.lang.UnsupportedOperationException: Cannot delete that URL: content://call_log/calls/922
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
at android.content.ContentProviderProxy.delete(ContentProviderNative.java:553)
at android.content.ContentResolver.delete(ContentResolver.java:1956)
at com.example.testbackup.ui.dataset1.Dataset1Fragment.deletePhoneCallLogs(Dataset1Fragment.java:425)
at com.example.testbackup.ui.dataset1.Dataset1Fragment.access$800(Dataset1Fragment.java:55)
at com.example.testbackup.ui.dataset1.Dataset1Fragment$2.onClick(Dataset1Fragment.java:150)
at android.view.View.performClick(View.java:7870)
at android.widget.TextView.performClick(TextView.java:14970)
at android.view.View.performClickInternal(View.java:7839)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29363)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7948)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

有人有解决方案吗?

好吧,我终于找到了如何删除所有电话,这篇文章帮助我:删除所有未接来电日志条目显然,我被迫指定呼叫的类型,以便删除

我的代码:

private void deletePhoneCallLogs() {
Cursor cursor = getContext().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
long id = cursor.getLong(cursor.getColumnIndexOrThrow(CallLog.Calls._ID));
Uri deleteUri = ContentUris.withAppendedId(CallLog.Calls.CONTENT_URI, id);
Log.d("PhoneCall","My delete PhoneCall"+deleteUri);
getContext().getContentResolver().delete(CallLog.Calls.CONTENT_URI , "type="+CallLog.Calls.TYPE , null);        }
cursor.close();
}

我在许多设备和安卓8和10上进行了测试。