Grant multiple permissions for android Marshmallow (Android 6.0)

0

I go straight to the problem. I want to request multiple permissions on Android 6.0 or higher versions in Android Studio

And it happens to me that even though I'm praying for you to ask everyone, just ask for the first one .. code

IMPORTS

import static android.Manifest.permission.CHANGE_CONFIGURATION;
import static android.Manifest.permission.MODIFY_AUDIO_SETTINGS;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; 
import static android.Manifest.permission.WRITE_SETTINGS;

MAIN

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Permis();
        }

Function to assign permissions

private void Permis() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if ((checkSelfPermission(WRITE_EXTERNAL_STORAGE) ==  PackageManager.PERMISSION_GRANTED) &&
                    (checkSelfPermission(CHANGE_CONFIGURATION) ==  PackageManager.PERMISSION_GRANTED) &&
                    (checkSelfPermission(MODIFY_AUDIO_SETTINGS) ==  PackageManager.PERMISSION_GRANTED) &&
                    (checkSelfPermission(WRITE_SETTINGS) ==  PackageManager.PERMISSION_GRANTED)) {

                Toast.makeText(tuneActivity.this, "Permiso concedido  anteriormente", Toast.LENGTH_SHORT).show();

            }


if ((shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE))||   (shouldShowRequestPermissionRationale(CHANGE_CONFIGURATION)) || (shouldShowRequestPermissionRationale(MODIFY_AUDIO_SETTINGS)) || (shouldShowRequestPermissionRationale(WRITE_SETTINGS))){

                DialogoAlerta();

            } else {
                requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,  CHANGE_CONFIGURATION, MODIFY_AUDIO_SETTINGS, WRITE_SETTINGS},
                        MY_PERMISSIONS_REQUEST);
            }


        }
    }

DialogoAlerta Function

private void DialogoAlerta() {

        AlertDialog.Builder dialogo=new AlertDialog.Builder(tuneActivity.this);
        dialogo.setTitle("Permisos desactivados");
        dialogo.setMessage("Debe aceptar los permisos para poder definir un  tono como ringtone o sonido de notificacion");
        dialogo.setPositiveButton("Aceptar", new  DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,  CHANGE_CONFIGURATION, MODIFY_AUDIO_SETTINGS, WRITE_SETTINGS},
                            MY_PERMISSIONS_REQUEST);
                }
            }

        });
    }

onRequestPermissionsResult

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
            'super.onRequestPermissionsResult(requestCode,permissions,grantResults);'
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED && grantResults[3] == PackageManager.PERMISSION_GRANTED ) { 

            Toast.makeText (tuneActivity.this,"Permiso concedido",Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText (tuneActivity.this,"Permiso no concedido",Toast.LENGTH_SHORT).show();
        }
        return;

    }
}
}

And as I was saying, it only asks for a permission which is WriteExternalStorage, that is the first one and when it enters the onRequestPermissionsResult, it clearly returns to me that the permissions have not been granted correctly since it does not verify them all because only the dialog box to allow the first, I hope your help guys from now thanks;)

    
asked by WhySoBizarreCode 23.04.2018 в 21:18
source

2 answers

1

As @ Elenasys said ♦ the only permissions that I needed were those of writing in memory and those of writing in the configuration, this last one being impossible to ask for risks that concidera android.

Then the solution to ask for Write SystemConfig permissions

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        retVal = Settings.System.canWrite(this); //esto nos retornara si ya tenemos los permisos obtenidos o no
if(retVal){  
            Toast.makeText(this, "Todo bien pasa", Toast.LENGTH_LONG).show();
        }else{
            //pedimos los permisos, pero como no podemos tenemos que llevar al usuario a las configuraciones donde estan los permisos de WriteConfiguration para que el mismo nos valide
            Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                        startActivity(intent);
        }

And so we can do what we want;), clearly can have variations in the code depending on how and what we want to use it. If you think of something more efficient or neat, post it.

    
answered by 24.04.2018 / 21:37
source
-1

Your code is correct, for operating system android 6.0 or later the way to require the permissions is correct but in this case, it is not enough just to import the classes, you must add the permissions within your file AndroidManifest.xml with this you must be able to visualize the dialogue without problem.

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

Another important point, the permissions:

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

are permissions that are granted only for system applications.

    
answered by 24.04.2018 в 00:58