Runtime Permissions Android

1

I'm having trouble getting the runtime permissions request, and only the permissions acceptance dialog for 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 permissions issue when executing the onClickButton method immediately displays the dialog to allow localization, but after allowing the crash app and the logs reports the following error regarding the lack of permission for SMS. However if I re-boot the app, now with the permission for the guaranteed location, and re-run the onClickButon method the dialog for the SMS permission is displayed.

  

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)

In addition to the log's I can see that the message is generated successfully, and by the impression of Toast 'Sent to 93xxxxxxx' I can see that the method was executed but SMS never really reaches its destination.

Someone can help me straighten this out so that I can have everything functional ...

    
asked by anonymous 14.02.2017 / 22:27

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 you use && to connect the two conditions it suffices that one of them is false to enter else .

What should be happening is that the application already has the ACCESS_COARSE_LOCATION permission, perhaps granted on a first run of the program, which causes that condition to be false and the sendMsg(); line to be executed.

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

When you need to request more than one permission at the same time, perhaps the easiest way to manage them is to request them in only 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 not guarantee that this is the only problem with your code since I do not have an overview of it.

Note: It does not make sense for permissions to be requested when the panic button is clicked. They should already have been granted before the user is in a panic situation.

    
14.02.2017 / 23:21