-1

I have an SMS App where now in 6.0 the user have to allow the app to send SMS. Right now i have this code which i got from link here.

 int MY_PERMISSIONS_REQUEST_READ_CONTACTS=0;
// Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.SEND_SMS)
                != PackageManager.PERMISSION_GRANTED) {

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

                // 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(this,
                        new String[]{Manifest.permission.SEND_SMS},
                        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.
            }
        }

But when i deny acess (if the user accidently hit the button) everythings will pretty much make the app crash (Cause sms buttons wont work). So when the user starts/reboot the app after the user have denied access i would like the app to ask again. Right now when i tried on my emulator no dialog would come up again so i had to go to settings/app and set permission. Or uninstall and install again.

Community
  • 1
  • 1
Tim
  • 547
  • 1
  • 7
  • 17
  • Possible duplicate of [Android marshmallow request permission?](http://stackoverflow.com/questions/33666071/android-marshmallow-request-permission) – Rohan Nov 15 '16 at 22:00

2 Answers2

0

What you are currently doing is asking the user for permission, but not checking if the permissions were actually granted. You can do this by implementing the onRequestPermissionsResult method in the activity from which you request the permissions.

See https://stackoverflow.com/a/34722591/3872500 for more info.

Community
  • 1
  • 1
Rohan
  • 1,180
  • 2
  • 15
  • 28
0

An example using a different permission from a working app with comments where you need to make your changes:

if (ContextCompat.checkSelfPermission(this,
        Manifest.permission.ACCESS_COARSE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
    progressLayout.setVisibility(View.GONE);
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.ACCESS_COARSE_LOCATION)) {

        //This is where you need to ask again if they want to allow the permission,
        //you should show a dialog here to ask if they're sure
        //and explain why you need the permission.
        requestDialog = new AlertDialog.Builder(this)
                .setTitle(getString(R.string.device_setup_location_permission_title))
                .setMessage(getString(R.string.device_setup_location_permission_message))
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //They clicked yes, they want to enable the permission,
                        //so let's show them the dialog again.
                        ActivityCompat.requestPermissions(DeviceSetupActivity.this,
                                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                                REQUEST_LOCATION_RESPONSE);
                    }
                })
                .setNegativeButton(getString(R.string.later), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //This means they don't want to grant the permission, 
                        //you should exit the app here, perhaps share another dialog saying that in
                        //order to use the app, the permission must be granted.
                        finish();
                    }
                })
                .show();

    } else {

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

        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                REQUEST_LOCATION_RESPONSE);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode == REQUEST_LOCATION_RESPONSE){
        if(hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)){
            startScan();
        }else{
            //exit the activity
            finish();
        }
    }
}

private boolean hasPermission(String perm) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return(PackageManager.PERMISSION_GRANTED==checkSelfPermission(perm));
    }
    return true;
}
wblaschko
  • 3,252
  • 1
  • 18
  • 24