4

Check permission dialog box appears again and again inspite of giving permission and clicking all dialog box as accept it closes the app.

I have a helper method (copied from here) to check multiple permissions and see if any of them are not granted.

            public static boolean hasPermissions(Context context, String... permissions) {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }
        return true;
    }
 int PERMISSION_ALL = 1;

         String[] PERMISSIONS = {Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CAMERA};
       if(!hasPermissions(this, PERMISSIONS)){
            Log.d("permission","permission");
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);

        }

I want it to load the same activity i.e. the MainActivity after granting permission.

Can anyone point why the permission is being asked multiple times.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
asifa
  • 771
  • 1
  • 28
  • 63
  • You should receive two prompts: one for `ACCESS_FINE_LOCATION` and one for `CAMERA`. If the user does not grant the permissions, your code is going to ask every time `onCreate` is called. Is this what you are seeing, or are you seeing something else? – Larry Schiefer Jul 14 '17 at 12:59
  • I am receiving two prompts: one for ACCESS_FINE_LOCATION and one for CAMERA. I am granting permission but still the permission prompts keeps appearing again and again like 4 or 5 times and then the app closes. – asifa Jul 14 '17 at 13:10
  • Is there a stacktrace? – Zoe Jul 14 '17 at 13:13
  • No errors in the logcat – asifa Jul 14 '17 at 13:14
  • If the app is closing and it is not intentional, there should be a crash dump in the logcat. Be sure to turn off any filtering for just your app in Android Studio's logcat view, otherwise you can miss the crash. – Larry Schiefer Jul 14 '17 at 13:22

3 Answers3

1

Kind of late to this Party but give this code a try

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        } else {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 666);  // Comment 26
        }
    }

    onAvail();
    onLoad();
}

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

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {

        case 666: // User selected Allowed  Permission Granted
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Snackbar s = Snackbar.make(findViewById(android.R.id.content), "Permission Granted", Snackbar.LENGTH_LONG);
                View snackbarView = s.getView();
                snackbarView.setBackgroundColor(Color.WHITE);
                TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
                textView.setTextColor(Color.RED);
                textView.setTextSize(18);
                textView.setMaxLines(6);
                s.show();

                // do your work here

                // User selected the Never Ask Again Option Change settings in app settings manually
            } else if (Build.VERSION.SDK_INT >= 23 && !shouldShowRequestPermissionRationale(permissions[0])) {

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
                alertDialogBuilder.setTitle("Change Permissions in Settings");
                alertDialogBuilder
                        .setMessage("Click SETTINGS to Manually Set\n\n" + "Permissions to use Database Storage")
                        .setCancelable(false)
                        .setPositiveButton("SETTINGS", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                Uri uri = Uri.fromParts("package", getPackageName(), null);
                                intent.setData(uri);
                                startActivityForResult(intent, 1000);
                            }
                        });

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();

            } else {
                // User selected Deny Dialog to EXIT App ==> OR <== RETRY to have a second chance to Allow Permissions
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
                alertDialogBuilder.setTitle("Second Chance");
                alertDialogBuilder
                        .setMessage("Click RETRY to Set Permissions to Allow\n\n" + "Click EXIT to the Close App")
                        .setCancelable(false)
                        .setPositiveButton("RETRY", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int id) {
                                Intent i = new Intent(MainActivity.this, MainActivity.class);
                                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(i);
                            }
                        })
                        .setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                finish();
                                dialog.cancel();
                            }
                        });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
    }
    break;
    }};
Vector
  • 3,066
  • 5
  • 27
  • 54
0

Just check that the permission you have mentioned here String[] PERMISSIONS = {Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CAMERA};

should be given in manifest file.

Mohd Saquib
  • 580
  • 4
  • 14
-1
Please do the following changes in the corresponding places.

 if (hasPermissions(this, PERMISSIONS)) {  
            // you have permissions  
            //Do your work.      
 }

replace **hasPermissions()** method with following code    

   public static boolean hasPermissions(Context context, String... permissions) {   
     int result;  
     List<String> listPermissionsNeeded = new ArrayList<>();  
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {  
            for (String permission : permissions) {  
                result = ContextCompat.checkSelfPermission(context, permission);  
                if (result != PackageManager.PERMISSION_GRANTED) {  
                    listPermissionsNeeded.add(permission);  
                }  
            }  
            if (!listPermissionsNeeded.isEmpty()) {  
                ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), PERMISSION_ALL);  
                return false;  
            }  
        }  
              return true;  
    }

replace **onRequestPermissionsResult()** with following code

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                       int[] grantResults) {
    Log.d("requestCode", String.valueOf(requestCode));
    switch (requestCode) {
        case PERMISSION_ALL: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                //Do your work.
                Toast.makeText(MainActivity.this, "Permission Recieved", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "Until you grant the permission, we cannot proceed further", Toast.LENGTH_SHORT).show();
            }
        }
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}