WhySoBizarreCode
WhySoBizarreCode

Reputation: 67

Ask multiples permissions android 6.0

I have to show multiple permissions and my program just show the first one i dont know why, i hope you can help me

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 Permis

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

Function DialogoAlerta

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

}

So.. the program just ask for the first permission'Write External Storage' and then it's shows 'permiso no concedido' but i dont know why, i hope you can explain me why, thanks and sorry for my bad english

Upvotes: 2

Views: 313

Answers (2)

WhySoBizarreCode
WhySoBizarreCode

Reputation: 67

Like @Sagar said, the only permission that i have to request is the "WRITE_EXTERNAL_STORAGE" because the "MODIFY_AUDIO_SETTINGS" is just considered "normal" for android. But returning to my problem one solution that i found is that:

the "WRITE_EXTERNAL_STORAGE" just asking it like i did, and in order to get the WRITE_SETTINGS i do that code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        retVal = Settings.System.canWrite(this);
if(retVal){  
            Toast.makeText(this, "All good", Toast.LENGTH_LONG).show();
        }else{
            Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                        startActivity(intent); // we open the part of the system configuration in which the person have to just enable our app ;)
        }

Upvotes: 0

Sagar
Sagar

Reputation: 24907

Permission request is only required for the permissions whose protection level is dangerous. You can refer this document to determine the protection level. Refer this permission overview document for more details.

MODIFY_AUDIO_SETTINGS

You don't see permission request for this because the protection level for this permission is normal. This permission will be granted automatically without need of requesting to user.

WRITE_SETTINGS

You don't see permission request because the protection level is signature. Based on the documentation:

Note: If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen. The app requests the user's approval by sending an intent with action ACTION_MANAGE_WRITE_SETTINGS. The app can check whether it has this authorization by calling Settings.System.canWrite().

You need to fire intent to request this permission.

CHANGE_CONFIGURATION

This permission requires app to be signed by firmware signing key or be installed on a system partition. So you cannot request this permission through permission dialog. Refer this SO

However there is a workaround to grant this permission though adb as follows:

adb -d shell pm grant <your package name> android.permission.CHANGE_CONFIGURATION

But you cannot expect all your users to have technical knowledge to do this.

Upvotes: 1

Related Questions