问多重权限android 6.0

时间:2018-04-23 23:27:37

标签: java android android-studio permissions android-permissions

我必须显示多个权限,我的程序只显示第一个我不知道为什么,我希望你能帮助我

进口

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

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

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

}

所以..程序只是要求第一个许可'写外部存储'然后它的节目&permso no concedido'但我不知道为什么,我希望你能解释我为什么,谢谢,抱歉我的英语不好

2 个答案:

答案 0 :(得分:1)

只有保护级别为dangerous的权限才需要权限请求。您可以参考此document来确定保护级别。有关详细信息,请参阅此permission overview document

MODIFY_AUDIO_SETTINGS

您没有看到此权限请求,因为此权限的保护级别为normal。此权限将自动授予,无需向用户请求。

WRITE_SETTINGS

您没有看到权限请求,因为保护级别为signature。基于documentation

  

注意:如果应用针对API级别23或更高级别,则应用用户必须通过权限管理屏幕向应用明确授予此权限。 该应用通过发送操作ACTION_MANAGE_WRITE_SETTINGS来请求用户的批准。应用可以通过调用Settings.System.canWrite()来检查是否具有此授权。

您需要触发意图才能请求此权限。

CHANGE_CONFIGURATION

此权限要求应用程序通过固件签名密钥签名或安装在系统分区上。因此,您无法通过权限对话框请求此权限。请参阅此SO

但是,有一种解决方法可以通过adb授予此权限,如下所示:

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

但您不能指望所有用户都具备相关技术知识。

答案 1 :(得分:0)

就像@Sagar所说,我必须要求的唯一许可是&#34; WRITE_EXTERNAL_STORAGE&#34;因为&#34; MODIFY_AUDIO_SETTINGS&#34;只考虑#34;正常&#34;对于Android。但回到我的问题,我找到的一个解决方案是:

&#34; WRITE_EXTERNAL_STORAGE&#34;就像我一样问它,为了得到WRITE_SETTINGS,我做了那个代码:

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class TaxCollection extends ResourceCollection
{
    private $pagination;

    public function __construct($resource)
    {
        $this->pagination = [
            'total' => $resource->total(),
            'count' => $resource->count(),
            'per_page' => $resource->perPage(),
            'current_page' => $resource->currentPage(),
            'total_pages' => $resource->lastPage()
        ];

        $resource = $resource->getCollection();

        parent::__construct($resource);
    }

    public function toArray($request)
    {
        return [
            'data' => $this->collection,
            'pagination' => $this->pagination
        ];
    }
}