-2

How to turn on app permissions programatically of Camera, Contacts, Location and storage etc. dynamically in Marshmallow.

I've the code, but it's open dialog box having buttons 'Deny' & 'Allow', I wants to turn op the app permissions directly, without dialog.

Code to 'Turn On' permission through dialog box, not directly..

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_welcome);

   requestPermission();

}


private void requestPermission(){
    if (ActivityCompat.shouldShowRequestPermissionRationale(WelcomeActivity.this,Manifest.permission.ACCESS_FINE_LOCATION)){
        Toast.makeText(WelcomeActivity.this,"1. GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
        ActivityCompat.requestPermissions(WelcomeActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
    } else {
        Toast.makeText(WelcomeActivity.this,"2. GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
        ActivityCompat.requestPermissions(WelcomeActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
    }
}


@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(WelcomeActivity.this, "Permission Granted, Now you can access location data.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(WelcomeActivity.this, "Permission Denied, You cannot access location data.", Toast.LENGTH_LONG).show();
            }
        break;
    }
}

2 Answers2

0

What you want to do is not possible. The whole idea of Marshmallow's permission system is to make the User able to decide whether they want to grant the permission. Plan your app to react accordingly when they do not grant it.

Kelevandos
  • 7,024
  • 2
  • 29
  • 46
  • ok for me, but it doesn't make sense user always go to app settings page and enable permissions, or show popup/dialog for every permission. – Andrew Ryan May 16 '16 at 10:00
  • Yes, this change was very controversial. It was introduced because many apps had a list of 20 permissions and the Users did not actually know what they need the permissions for. Now Google wants the Users to be aware what a permission actually does and to remove the scary, long permission lists. – Kelevandos May 16 '16 at 10:02
  • Google decided so, we have to adjust this and redesign the apps to consider that. Easy as that. – Kelevandos May 16 '16 at 10:03
  • ok, as I need multiple permissions, don't we've any shortest way provide to user, as he click only once, and turn on all permissions. – Andrew Ryan May 16 '16 at 10:12
  • You can unlock multiple permissions within a single permission group, check the documentation for that. But what is in which group - you can't control :( Generally, you should simply ask the user the first time the given permission would be needed and prepare your app flow for a situation where they say no (many users who are not technical will actually deny permission without thinking much about it. – Kelevandos May 16 '16 at 10:53
  • If I answered your question, please mark the answer as correct :-) And good luck with the permissions - they are less trouble to implement than they seem :-) – Kelevandos May 16 '16 at 12:55
0

Your users should have the option of "don't ask me again" which can grant automatic permission upon request. You need to make a convincing case in your add for that in your app, and then catch whenever you get a permission fail

netsplit
  • 1,070
  • 1
  • 10
  • 18
  • okay, but still in my need to give much permissions, don't we've any another way to give a option to user on a single hit, just turn all permissions. – Andrew Ryan May 16 '16 at 10:13