asifa
asifa

Reputation: 771

Permission request dialog box appearing multiple times android

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.

Upvotes: 4

Views: 4073

Answers (3)

Vector
Vector

Reputation: 3235

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;
    }};

Upvotes: 1

Kolla RameshReddy
Kolla RameshReddy

Reputation: 61

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);
    }
}

Upvotes: -1

Mohd Saquib
Mohd Saquib

Reputation: 590

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.

Upvotes: 0

Related Questions