For writing a CallLog, I have used the following code:
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);
}
The onClick()
method is declared as callLog()
in the xml-file in the following way:
<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"
/>
I have added <uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
in the manifest.
But, still, I get the following error:
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)
What is going wrong and how should I avoid this?
Well, I got it, thanks to https://stackoverflow.com/a/47429378 this answer, the code started working.
The problem was that the permissions needed to be asked at runtime, and this resolved the problem completely!
Previously, I was asking the permissions only through the manifest file of the android.