Android 动态权限申请

2019-08-15  本文已影响0人  YoungEveryday

一、需求

开发Android首先要动态提示用户授权,比如访问摄像头、读取本地照片等等,这些都需要用户动态提供权限,然后程序才能有权限动态操作。

二、流程分析

①在AndroidManifest.xml添加需要的权限列表

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.CALL_PHONE"/>

②创建Activity,在Activity中创建权限数组

private String[] str = new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CALL_PHONE
    };

③进行权限申请

private void checkPermissions(String[] str) {
        //发现需要申请的权限
        List<String> needPermissions = findNeedPermissions(str);
        if(null!=needPermissions&&needPermissions.size()>0){
            ActivityCompat.requestPermissions(this,
                    needPermissions.toArray(new String[needPermissions.size()]),
                    REQUEST_CODE);
        }

    }
private List<String> findNeedPermissions(String[] str){
        for(String string:str){
            if (ContextCompat.checkSelfPermission(this,
                    string) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.shouldShowRequestPermissionRationale(
                    this, string)) {
                list_needPermission.add(string);
            }
        }
        return list_needPermission;
    }

④动态监听用户是否授权,如果用户没有授权,弹出框提示用户没有授权,并且可以直接点击设置跳转到设置页面进行授权

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CODE) {
            if(!verifyPermissions(grantResults)){
                showMissingPermissionDialog();
            }
        }

    }
//判断用户是否授权
private boolean verifyPermissions(int[] grantResults) {
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }
//没有授权弹出AlertDialog
private void showMissingPermissionDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("当前应用缺少必要权限。\n\n请点击\"设置\"-\"权限\"-打开所需权限。");

        // 拒绝, 退出应用
        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });

        builder.setPositiveButton("设置",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startAppSettings();
                    }
                });

        builder.setCancelable(false);

        builder.show();
    }
//跳转到设置页面
private void startAppSettings() {
        Intent intent = new Intent(
                Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }
上一篇下一篇

猜你喜欢

热点阅读