Permission Denial: not allowed to send broadcast android.intent.action.PHONE_STATE from pid=5646, uid=11219

34 Views Asked by At

I am facing a problem regarding Android OS. I want to detect incoming calls, but from API >=30 or Android>=11 devices, after the user grants the permission the application crashes, And Logcat shows the following exception.

FATAL EXCEPTION: main
 Process: com.example.callerdetecter, PID: 11623
 java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.PHONE_STATE from pid=11623, uid=11219
 at android.os.Parcel.createExceptionOrNull(Parcel.java:2374)
 at android.os.Parcel.createException(Parcel.java:2358)
 at android.os.Parcel.readException(Parcel.java:2341)
 at android.os.Parcel.readException(Parcel.java:2283)
 at android.app.IActivityManager$Stub$Proxy.broadcastIntentWithFeature(IActivityManager.java:5623)
 at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1115)
 at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:468)
 at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:468)
 at com.example.callerdetecter.MainActivity.prepareBroadcast(MainActivity.java:73)
 at com.example.callerdetecter.MainActivity.lambda$onCreate$0$com-example-callerdetecter-MainActivity(MainActivity.java:33)
 at com.example.callerdetecter.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
 at android.view.View.performClick(View.java:7503)
 at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1218)
 at android.view.View.performClickInternal(View.java:7480)
 at android.view.View.access$3600(View.java:813)
 at android.view.View$PerformClick.run(View.java:28445)
 at android.os.Handler.handleCallback(Handler.java:938)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:223)
 at android.app.ActivityThread.main(ActivityThread.java:7950)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:603)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: android.os.RemoteException: Remote stack trace:
 at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:16429)
 at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:162
 at com.android.server.am.ActivityManagerService.broadcastIntentWithFeature(ActivityManagerService.java:17116)
 at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2306)
 at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2926)

1

There are 1 best solutions below

2
Dan Gerchcovich On

When you prompt the user for this runtime permission, do you have a guard in there, to check if the user has enabled the permission or not, and if they haven't to avoid broadcasting the intent?

You want to make sure the Phone call detection feature is only active if the permission is enabled first.

You might be triggering the feature before the permission is enabled, hence why android is throwing a Permission denial error.

Can you post some code? Anything you can?

When you prompt the user for this runtime permission, do you have a guard in there, to check if the user has enabled the permission or not, and if they haven't to avoid broadcasting the intent?

You want to make sure the Phone call detection feature is only active if the permission is enabled first.

You might be triggering the feature before the permission is enabled, hence why android is throwing a Permission denial error.

Can you post some code? Anything you can?

UPDATE

Your code here, when you prompt for the runtime permission.

 ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_PHONE_STATE,
                    android.Manifest.permission.READ_CALL_LOG,
                    Manifest.permission.READ_CONTACTS}, REQUEST_READ_PHONE_STATE);

Looks like you're prompting for multiple permissions under the same request code. You might have to check that each of the permissions have been granted, before running the prepareBroadcast API. So only when each permission in the grantResults collection has a granted state (or at least the ones required for the intent to work).

Please check if the grantResults collection in the OnActivityResult has a count greater than 1, and check if each permission has a granted state.