2

Marshmallow Runtime Permission onRequestPermissionsResult was nor Called While running playstore Build Apk but working fine the normal debuging Apk.Anyone help me..Thanks.

Here is my code

private void EnablePermissions() 
{
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.RECORD_AUDIO)) {

    } else {

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

        ActivityCompat.requestPermissions(this, new String[] { android.Manifest.permission.RECORD_AUDIO },
                MY_PERMISSIONS_REQUEST_RECORD);
        Toast.makeText(MainActivity.this, "Permission Request", Toast.LENGTH_SHORT).show();
        // result of the request.
    }
    // Add a marker in Sydney, Australia, and move the camera.
    if (ContextCompat.checkSelfPermission(MainActivity.this,
            android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(MainActivity.this, "checkSelfPermission ", Toast.LENGTH_SHORT).show();
        return;
    } else {
        Log.d("Permission Denied", "Permission Failed to enable");
        Toast.makeText(MainActivity.this, "Permission Failed to enable", Toast.LENGTH_SHORT).show();
    }
}

OnRequestPermissionResult Method

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    try {
        switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_RECORD: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                ChatActivity();

            } else {
                Toast.makeText(MainActivity.this, "Permission denied Chat ", Toast.LENGTH_SHORT).show();
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }
        // other 'case' lines to check for other
        // permissions this app might request
        }

    } catch (Exception e) {
        Log.e(Constants.LOG, e.getMessage());
    }
}
Community
  • 1
  • 1

1 Answers1

3

This is the right way of doing it https://stackoverflow.com/a/35495372/4493133 . I am doing basically the same thing below.

Here is an example on how I made it work for my code

private void requestStoragePermission() {
        /*
         We don't have permission so prompt the user
         If permission is not granted control goes to onRequestPermissionResult
          */
        ActivityCompat.requestPermissions(
                this,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }

onRequestPermissionsResult method:

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        switch (requestCode) {
            case REQUEST_EXTERNAL_STORAGE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d("PERMISSION", "Storage permission granted");
                } else {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

                        showMessageOkCancel("You need to allow access to Storage to use the offline timetables",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                        switch (which) {
                                            case DialogInterface.BUTTON_POSITIVE:
                                                requestStoragePermission();
                                                break;
                                            case DialogInterface.BUTTON_NEGATIVE:
                                                Toast.makeText(MainActivity.this, "Storage permission required to continue", Toast.LENGTH_LONG)
                                                        .show();
                                                finishAffinity();
                                                break;
                                        }
                                    }
                                });
                    } else {
                        Toast.makeText(this, "Go to settings and enable storage permissions", Toast.LENGTH_LONG)
                                .show();
                        finishAffinity();
                    }
                }

        }

    }

showMessageOkCancel method:

private void showMessageOkCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", okListener)
                .create()
                .show();
    }

Explanation:

In your main code, you only requestStoragePermission().

onRequestPermissionsResult will run whether the permission is granted or not.

If the permission is granted, then good, that's all.

If the permission is not granted, then shouldShowRequestPermissionRationale() will return true if it is the first time asking for permission. I then prompt another dialog with some explanation to give another chance to the user to enable the permission.

However if "don't ask again" is checked, then shouldShowRequestPermissionRationale will return false. The requestPermissions dialog will not pop up anymore. The user has to go to enable the permission manually.

Note you don't have to finishAffinity(). You can just disable the portion of the code that needs that permission.

Community
  • 1
  • 1
  • I am using the above method also but i have same problem in Activity and fragment Activity OnRequestPermissionResult not called. – Shobana Velmurugan Sep 09 '16 at 06:49
  • EnablePermissions() should only have what I did in requestStoragePermission(). The rest of the code is in onRequestPermissionsResult(). Make sure you put onRequestPermissionsResult() in the Activity that calls EnablePermissions(). – JohnMadon1234567890 Sep 09 '16 at 22:23
  • Then you only call EnablePermissions() in your code, it should work... If it doesn't show me your code – JohnMadon1234567890 Sep 09 '16 at 22:26