Runtime Permissions Android

Asked

Viewed 400 times

1

I am having some difficulty in being able to request the permissions in Runtime, only the permissions accepting dialog of one of the two required permissions is shown when I click the button.

 /*Button PANIC Button
    @ Click this button to send a PANIC message to defined contact
    @ Inside the message go a PANIC message, name of person and GPS location
    */
    Button button_panic = (Button) findViewById(R.id.button_panic);
    button_panic.setOnClickListener(new OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        public void onClick(View v) {

            if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED)
            {
                requestLocationPermission();
                requestSMSPermission();

                String Message = "GRANTED";
                Log.i("Location permission", Message);


            }
            else
            {
                sendMsg();
            }

        }

    });
}

@RequiresApi(api = Build.VERSION_CODES.M)
private void getLocation() {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.


        //requestLocationPermission();

        return;
    }
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);

    String longitudeString = String.valueOf(location.getLongitude());
    String latitudeString = String.valueOf(location.getLatitude());

    message = "PANIC - Preciso de Ajuda " + Nome + " " + Apelido + ". " + "Esta é a minha localização: Latitude: " + latitudeString + " Longitude: " + longitudeString;

    Log.e("Log Message", message);

}

@RequiresApi(api = Build.VERSION_CODES.M)
private void sendMsg() {

    getLocation();

    SmsManager smsManager = SmsManager.getDefault();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
        requestSMSPermission();
    }

    if (location != null) {
        smsManager.sendTextMessage(Telefone, null, message, null, null);
        Toast.makeText(MainActivity.this, "Sent to " + Telefone, Toast.LENGTH_SHORT).show();
    } else {
        smsManager.sendTextMessage(Telefone, null, "test", null, null);
        Toast.makeText(MainActivity.this, "Please open your location service", Toast.LENGTH_SHORT).show();
    }
}

@RequiresApi(api = Build.VERSION_CODES.M)
private void requestSMSPermission() {
    requestPermissions(new String[]{Manifest.permission.SEND_SMS}, REQUEST_PERMISSION_SEND_SMS_CODE);
}

@RequiresApi(api = Build.VERSION_CODES.M)
private void requestLocationPermission() {
    requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_PERMISSION_FINE_LOCATION_CODE);
    requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_PERMISSION_COARSE_LOCATION_CODE);
}

The question of permissions when executing the onClickButton method is immediately presented the dialog to allow the location, but after allowing the app crasha and by the logs reports the following error regarding the lack of permission for SMS. However if I boot the app again, now already with the permission for the guaranteed location, and run again the onClickButon method is presented the dialog for SMS permission.

java.lang.Securityexception: Sending SMS message: uid 10229 does not have android.permission.SEND_SMS. at android.os.Parcel.readException(Parcel.java:1620) at android.os.Parcel.readException(Parcel.java:1573) at com.android.Internal.telephony.Isms$Stub$Proxy.sendTextForSubscriber(Isms.java:768) at android.telephony.Smsmanager.sendTextMessageInternal(Smsmanager.java:333) at android.telephony.Smsmanager.sendTextMessage(Smsmanager.java:299)

Also by the log’s I can see that the message is generated successfully, and by the printing of Toast 'Sent to 93xxxxxxx' I can see that the method was executed but in reality SMS never reaches the destination.

Can someone help me set this up so I can get everything functional...

  • In the manifest it’s all right right right? You’re giving permission normally?!

  • Yes in the manifest is right... It is something related to these Runtime permissions required by sdk 23 or higher... I don’t know how to fix this situation and I don’t know why then the SMS doesn’t reach the destination although everything seems to go well with sending

  • You tried to add Activitycompat this way: Activitycompat.requestPermissions(new S...)

1 answer

3

One problem I see is how the permissions test is being done:

if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
        ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED)

When using && to connect the two conditions just one of them is false to enter the else.

What should be happening is that the application already has the permission ACCESS_COARSE_LOCATION, which makes that condition false and the line sendMsg(); be executed.

Also remember that the process of asking for permission is asynchronous and its success is only known after the method onRequestPermissionsResult() be called.

When you need to ask for more than one permission at the same time, maybe the easiest way to manage it is to ask for it in just one call to requestPermissions():

ActivityCompat.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                                               Manifest.permission.ACCESS_COARSE_LOCATION,
                                               Manifest.permission.SEND_SMS},
                                  REQUEST_ALL_PERMISSION);

Note: I can’t guarantee that this is the only problem with your code since I don’t have an overview of it.

Observing: It makes no sense for permissions to be requested when the panic button be clicked. They must have been granted before the user is in a panic situation.

Browser other questions tagged

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