以编程方式删除所有 phone 通话记录

Delete all phone call logs programmatically

我正在尝试制作一种一键删除所有通话记录的方法。 这是我的代码:

    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.access0(Dataset1Fragment.java:55)
    at com.example.testbackup.ui.dataset1.Dataset1Fragment.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.access00(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)

有人有解决办法吗?

好吧,我终于找到了删除所有电话的方法,这个post帮帮我:Delete all Missed Calls log entries 显然,我被迫为调用指定一种类型,以便将其删除

我的代码:

    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();
}

我在许多设备上以及 android 8 和 10 上进行了测试。