在 android 中写入 phone-callLogs 时出错

Error while writing phone-callLogs in android

为了写一个CallLog,我使用了下面的代码:

public void callLog(View view){
    //textView.setText("Call Logging Started ... ");
    ContentValues values = new ContentValues();
    values.put(CallLog.Calls.NUMBER, 1234567890);
    values.put(CallLog.Calls.DATE, System.currentTimeMillis());
    values.put(CallLog.Calls.DURATION, 0);
    values.put(CallLog.Calls.TYPE, CallLog.Calls.OUTGOING_TYPE);
    values.put(CallLog.Calls.NEW, 1);
    values.put(CallLog.Calls.CACHED_NAME, "");
    values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0);
    values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "");
    getContentResolver().insert(CallLog.Calls.CONTENT_URI, values);
}

onClick() 方法在 xml 文件中声明为 callLog(),方法如下:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="132dp"
    android:layout_marginTop="128dp"
    android:onClick="callLog"
    android:text="Add call log"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
 />

我在清单中添加了 <uses-permission android:name="android.permission.WRITE_CALL_LOG"/>

但是,我仍然收到以下错误:

java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
        at android.view.View.performClick(View.java:7448)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7425)
        .
        .
        .
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
        at android.view.View.performClick(View.java:7448) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
        at android.view.View.performClickInternal(View.java:7425) 
        .
        .
        .
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
     Caused by: java.lang.SecurityException: Permission Denial: writing com.android.providers.contacts.CallLogProvider uri content://call_log/calls from pid=5389, uid=10178 requires android.permission.WRITE_CALL_LOG, or grantUriPermission()
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2373)
        at android.os.Parcel.createException(Parcel.java:2357)
        at android.os.Parcel.readException(Parcel.java:2340)
        .
        .
        .
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

出了什么问题,我应该如何避免这种情况?

好吧,我明白了,感谢 这个答案,代码开始工作了。
问题是需要在运行时询问权限,这完全解决了问题!
以前,我仅通过 android.

的清单文件询问权限