Grant permissions for android Marshmallow (Android 6.0)

1

I am trying to grant permissions on Android Marshmallow but it still does not work. The code that I used is this:

  private  boolean checkAndRequestPermissions() {
      int pocatioPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_SETTINGS);
    int permi = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    List<String> listPermissionsNeeded = new ArrayList<>();

    if (permi != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }
    if (pocatioPermission != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(Manifest.permission.WRITE_SETTINGS);
    }

    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
        return false;
    }
    return true;
}

But in the console I still see this error:

  

was not granted this permission: android.permission.WRITE_SETTINGS

Thank you very much for the answers, but I still can not get it to work. I have put the permissions on the manifest and now I have added the Settings.System.canWrite () but it still gives me the same error, the strange thing is that when I start the app the windows should appear to grant the permissions and it only appears for the permission WRITE_EXTERNAL_STORAGE but for the WRITE_SETTINGS it does not show me any little windows to grant permissions.

    
asked by Hugo AE 10.11.2016 в 19:57
source

2 answers

1

Verify that:

  • You have <uses-permission> in your manifest.
  • Call the Settings.System.canWrite() function to see if you are able to write.
  • If this last event returns false:

    if (Settings.System.canWrite(context) {
            //Todo bien aqui
    } else {
        Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
        intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
    

The documentation for ACTION_MANAGE_WRITE_SETTINGS says that:

  

ACTION_MANAGE_WRITE_SETTINGS   Added in API level 23     String ACTION_MANAGE_WRITE_SETTINGS   Activity Action: Show screen for controlling which apps are allowed to write / modify system settings.

     

In some cases, a matching Activity may not exist, so ensure you safeguard against this.

     

Input: Optionally, the Intent's data URI can specify the application package name to directly invoke the management GUI specific to the package name. For example "package: com.my.app".

     

Output: Nothing.

     

Constant Value: "android.settings.action.MANAGE_WRITE_SETTINGS"

Spanish (summarized):

  

Displays the screen to control those applications that can read or modify the system configuration.

    
answered by 10.11.2016 в 20:39
0

Ensure that you have defined the permission within your AndroidManifest.xml :

   <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

and using Settings.System.canWrite () You can check if your app can actually change the settings of the system, check the documentation from API 23 an application can not change the settings unless you declare WRITE_SETTINGS in your AndroidManifest.xml :

 boolean permission;
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            permission = Settings.System.canWrite(context);
        } else {
            permission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
        }
    
answered by 11.11.2016 в 17:44