Search code examples
javaandroidphone-callcalllog

Delete all phone call logs programmatically


I'm trying to make a method to delete all call logs with one click. Here is my code:

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

My manifest for the permissions :

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

I have an error that I don't understand, which is this one:

    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)

Does anyone have the solution ?


Solution

  • Well, I finally found how to delete all the phonecalls, this post help me :Delete all Missed Calls log entries Apparently I am forced to specify a type for the calls so that it gets removed

    My code :

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

    I tested on many devices and on android 8 and 10.