我们看到过很多手机,在你需要本地存储或照相的时候,会弹出一个是否允许获得权限的弹框。

如下图::

动态再次获得权限

其实并不是所有的手机都会出现,Android7.0版本,或者API 23 以上(包含23)都是需要进行动态获取判断,否则项目一遇到这样的手机就会闪退,所有我们适配时就会考虑到所有的肯能性。


虽然我们要动态获取,但是,我的清单文件中,这些权限也是比不可少的。我就拿访问本地权限和摄像头、音频的权限做例子吧:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
看看我们的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.quanxian.open.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/save"
        android:text="动态获得存储的权限"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:id="@+id/paishe"
        android:text="动态获得拍摄的权限"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:id="@+id/save_paishe"
        android:text="同时获得存储、拍摄、音频的权限"/>
</LinearLayout>
然后我们,获得些控件,开始操作:

首先,我们需要定义几个数组的变量,用来存放动态添加的权限::

// 本地访问申请的权限
private String[] cuncu_quanxian = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
//摄像头的权限
private String[] paishe_quanxian={Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO};
//本地访问和摄像头权限同时获得
private String[] save_and_paishs={Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO};
我们点击这些按钮时,需要先判断他的API是否符合需要动态添加的条件

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.save://动态获取访问本地存储的权限
            // 版本判断。当手机系统大于 23 时,才有必要去判断权限是否获取
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                // 检查该权限是否已经获取
                int i = ContextCompat.checkSelfPermission(this, cuncu_quanxian[0]);
                // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
                if (i != PackageManager.PERMISSION_GRANTED) {
                    // 如果没有授予该权限,就去提示用户请求
                    startRequestPermission(cuncu_quanxian,111);
                }else{
                    Toast.makeText(MainActivity.this,"用户已经授予存储权限",Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(MainActivity.this,"用户api小于23,不需要重新授权",Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.paishe://动态获取摄像头的权限
            // 版本判断。当手机系统大于 23 时,才有必要去判断权限是否获取
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                // 检查该权限是否已经获取
                int i = ContextCompat.checkSelfPermission(this, paishe_quanxian[0]);
                // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
                if (i != PackageManager.PERMISSION_GRANTED) {
                    // 如果没有授予该权限,就去提示用户请求
                    startRequestPermission(paishe_quanxian,222);
                }else{
                    Toast.makeText(MainActivity.this,"用户已经授予存储权限",Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(MainActivity.this,"用户api小于23,不需要重新授权",Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.save_paishe://动态获取本地和摄像头的权限
            // 版本判断。当手机系统大于 23 时,才有必要去判断权限是否获取
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                // 检查该权限是否已经获取
                int i = ContextCompat.checkSelfPermission(this, save_and_paishs[0]);
                // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
                if (i != PackageManager.PERMISSION_GRANTED) {
                    // 如果没有授予该权限,就去提示用户请求
                    startRequestPermission(save_and_paishs,333);
                }else{
                    Toast.makeText(MainActivity.this,"用户已经授予存储权限",Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(MainActivity.this,"用户api小于23,不需要重新授权",Toast.LENGTH_SHORT).show();
            }
            break;
    }
}

调用方法:

// 开始提交请求权限
private void startRequestPermission(String[] quanxian,int code) {
    ActivityCompat.requestPermissions(this, quanxian, code);
}

其实到这边,我们就已经能实现弹出框的操作,但是不够完美,如果用户选择的“禁止”呢??

那么就用到我们的回调方法:::

 // 用户权限 申请 的回调方法
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 111) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    // 判断用户是否 点击了不再提醒。(检测该权限是否还可以申请)
                    boolean b = shouldShowRequestPermissionRationale(cuncu_quanxian[0]);
                    if (!b) {
                        // 用户还是想用我的 APP 的
                        // 提示用户去应用设置界面手动开启权限
                        dialog = new AlertDialog.Builder(this)
                                .setTitle("存储权限不可用")
                                .setMessage("请在-应用设置-权限-中,允许此权限来保存用户数据")
                                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // 跳转到应用设置界面
                                        goToAppSetting();
                                        dialog.dismiss();
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).setCancelable(false).show();
//                        Toast.makeText(MainActivity.this,"您已经禁止了存储权限,请到“权限管理”中开启",Toast.LENGTH_SHORT).show();
                    } else{

                    }

                } else {
                    Toast.makeText(this, "权限获取成功", Toast.LENGTH_SHORT).show();
                }
            }
        }
        if(requestCode==222){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED||grantResults[1] != PackageManager.PERMISSION_GRANTED) {
                    // 判断用户是否 点击了不再提醒。(检测该权限是否还可以申请)
                    boolean b = shouldShowRequestPermissionRationale(paishe_quanxian[0]);
                    if (!b) {
                        // 用户还是想用我的 APP 的
                        // 提示用户去应用设置界面手动开启权限
                        dialog = new AlertDialog.Builder(this)
                                .setTitle("存储权限不可用")
                                .setMessage("请在-应用设置-权限-中,允许存储权限来保存用户数据")
                                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // 跳转到应用设置界面
                                        goToAppSetting();
                                        dialog.dismiss();
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).setCancelable(false).show();
//                        Toast.makeText(MainActivity.this,"您已经禁止了存储权限,请到“权限管理”中开启",Toast.LENGTH_SHORT).show();
                    } else{

                    }
                    boolean y=shouldShowRequestPermissionRationale(paishe_quanxian[1]);
                    if (!y) {
                        // 用户还是想用我的 APP 的
                        // 提示用户去应用设置界面手动开启权限
                        dialog = new AlertDialog.Builder(this)
                                .setTitle("音频权限不可用")
                                .setMessage("请在-应用设置-权限-中,允许音频权限来保存用户数据")
                                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // 跳转到应用设置界面
                                        goToAppSetting();
                                        dialog.dismiss();
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).setCancelable(false).show();
//                        Toast.makeText(MainActivity.this,"您已经禁止了存储权限,请到“权限管理”中开启",Toast.LENGTH_SHORT).show();
                    } else{

                    }
                } else {
                    Toast.makeText(this, "权限获取成功", Toast.LENGTH_SHORT).show();
                }
            }
        }
        if(requestCode==333){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED||grantResults[1] != PackageManager.PERMISSION_GRANTED||grantResults[2] != PackageManager.PERMISSION_GRANTED) {
                    // 判断用户是否 点击了不再提醒。(检测该权限是否还可以申请)
                    boolean b = shouldShowRequestPermissionRationale(save_and_paishs[0]);
                    if (!b) {
                        // 用户还是想用我的 APP 的
                        // 提示用户去应用设置界面手动开启权限
                        dialog = new AlertDialog.Builder(this)
                                .setTitle("存储权限不可用")
                                .setMessage("请在-应用设置-权限-中,允许存储权限来保存用户数据")
                                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // 跳转到应用设置界面
                                        goToAppSetting();
                                        dialog.dismiss();
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).setCancelable(false).show();
//                        Toast.makeText(MainActivity.this,"您已经禁止了存储权限,请到“权限管理”中开启",Toast.LENGTH_SHORT).show();
                    } else{

                    }
                    boolean p=shouldShowRequestPermissionRationale(save_and_paishs[1]);
                    if (!p) {
                        // 用户还是想用我的 APP 的
                        // 提示用户去应用设置界面手动开启权限
                        dialog = new AlertDialog.Builder(this)
                                .setTitle("摄像头权限不可用")
                                .setMessage("请在-应用设置-权限-中,允许摄像头权限来保存用户数据")
                                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // 跳转到应用设置界面
                                        goToAppSetting();
                                        dialog.dismiss();
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).setCancelable(false).show();
//                        Toast.makeText(MainActivity.this,"您已经禁止了存储权限,请到“权限管理”中开启",Toast.LENGTH_SHORT).show();
                    } else{

                    }

                    boolean y=shouldShowRequestPermissionRationale(save_and_paishs[2]);
                    if (!y) {
                        // 用户还是想用我的 APP 的
                        // 提示用户去应用设置界面手动开启权限
                        dialog = new AlertDialog.Builder(this)
                                .setTitle("音频权限不可用")
                                .setMessage("请在-应用设置-权限-中,允许音频权限来保存用户数据")
                                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // 跳转到应用设置界面
                                        goToAppSetting();
                                        dialog.dismiss();
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }).setCancelable(false).show();
//                        Toast.makeText(MainActivity.this,"您已经禁止了存储权限,请到“权限管理”中开启",Toast.LENGTH_SHORT).show();
                    } else{

                    }
                } else {
                    Toast.makeText(this, "权限获取成功", Toast.LENGTH_SHORT).show();
                }
            }
        }

如果用户选择的是“禁止”,我们可以进步操作,跳转到手机的权限管理界面去设置::

// 跳转到当前应用的设置界面
private void goToAppSetting() {
    Intent localIntent = new Intent();
    localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (Build.VERSION.SDK_INT >= 9) {
        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        localIntent.setData(Uri.fromParts("package", getPackageName(), null));
    } else if (Build.VERSION.SDK_INT <= 8) {
        localIntent.setAction(Intent.ACTION_VIEW);
        localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
        localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
    }
    startActivity(localIntent);
}

这共能的代码基本完成了,如有看不懂的,可以下载本人的Demo

相关文章:

  • 2021-04-09
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
  • 2021-05-09
  • 2021-06-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-20
  • 2021-10-11
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2021-11-20
  • 2021-07-04
相关资源
相似解决方案