"java.lang.Securityexception: Permission Denial" on Android 6 Marshmallow (API 23)

Asked

Viewed 2,979 times

11

I’m trying to register a broadcastreceiver so I can check a new sms that might arrive on the device.

I’m getting an error only on android marshmallow in the following snippet of my code:

    public static final String BROADCAST = "android.provider.Telephony.SMS_RECEIVED";
    .
    .
    .
    Intent intent = new Intent(BROADCAST);
    getActivity().sendBroadcast(intent);//erro nessa linha

Error:

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.provider.Telephony.SMS_RECEIVED from pid=10506, uid=10064

1 answer

11


As of version 6.0 (API level 23), permissions considered "dangerous"(Dangerous Permissions) are validated during the execution of the application and not when installing. This does not invalidate that they do not have to be declared in the Androidmanifest.xml.

The applications with targetApi 23+ that require Dangerous Permissions , to run on Android devices 6.0 or higher, you will need to adjust to this new approach.

Whenever the application executes an operation that requires a permission of this type it has to check if it has it before executing it.

This check is done by calling the method ContextCompat.checkSelfPermission():

int permissionCheck = ContextCompat.checkSelfPermission(context,
                                                        Manifest.permission.RECEIVE_SMS);

If the application has the permission is returned PackageManager.PERMISSION_GRANTED otherwise returns PackageManager.PERMISSION_DENIED

If the application does not have the permission it is necessary to request it using the method ActivityCompat.requestPermissions():

private final int PERMISSIONS_REQUEST_RECEIVE_SMS = 1;
ActivityCompat.requestPermissions(context,
                                  new String[]{Manifest.permission.RECEIVE_SMS},
                                  PERMISSIONS_REQUEST_RECEIVE_SMS);

An dialog to ask the user for permission.
The dialog is shown asynchronously, the method being onRequestPermissionsResult() called when the user responds.

That method should be implemented to address the:

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSIONS_REQUEST_RECEIVE_SMS: {
            // Se o pedido foi cancelado o array está vazio.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permissão foi concedida
                // pode fazer o que pretende

            } else {

                // permissão foi negada

            }
            return;
        }

        // outros 'case' se tiver outras permissões para verificar
    }
}

For more detailed information see documentation.

  • 1

    +1 Great explanation!

  • 1

    Thanks ramaral I was reading the documentation after I posted the question. On this link https://github.com/googlesamples/android-RuntimePermissions there are examples from google of how to do this. (leaving the link in case someone finds it useful)

  • Sir, the dialog is open automatically or you have to do some XML for that?

  • 1

    It opens automatically. You don’t have to call me "Sir":)

  • Ah I don’t get kk anymore let go of my app :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.