-1

I am working on Android application that needs to be notified when phone receives a call. I use approach with BroadcastReceiver since I want to get notification about incoming call even when application is not active. Therefore, approach with using TelephonyManager and PhoneStateListener does not suit my needs. So, my application has appropriate permission in manifest:

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

It also properly registers broadcast receiver in manifest:

<receiver android:enabled="true" android:name=".CallReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
</receiver>

Class CallReceiver implements BroadcastReceiver, but once I start application, its method onReceive of CallReceiver never gets invoked the is never invoked. Reason for this is that system denies to deliver Intent to my broadcast receiver, as I found following messages in log each time phone rings:

W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to com.example.incomingcall/.CallReceiver requires android.permission.READ_PRIVILEGED_PHONE_STATE due to sender android (uid 1000)
W/BroadcastQueue: Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to com.example.incomingcall/.CallReceiver requires android.permission.READ_PHONE_STATE due to sender android (uid 1000)

As I explained, I already put READ_PHONE_STATE permission in the manifest, while other permission specified here is system permission that can't be placed into the manifest. Any ideas how to overcome this problem? Device on which this appears in Nexus 6p with Android 6.0.1

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
dstefanox
  • 2,222
  • 3
  • 19
  • 21

1 Answers1

-2
if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_PHONE_STATE)!= PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,Manifest.permission.READ_PHONE_STATE)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_PHONE_STATE},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}
Shubham
  • 521
  • 4
  • 11