Android 权限相关

简述

本文简要记录运行时权限注意点

1、检查是否具有某种权限
// 如检查是否具有相机权限....
ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
// Packmanager 常量
// 授权
public static final int PERMISSION_GRANTED = 0;
// 未授权
public static final int PERMISSION_DENIED = -1;
2、申请权限
// 123自定义的返回码
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},123);
3、用户拒绝权限显示UI让用户去申请

1.如果应用之前请求过此权限但用户拒绝了请求,此方法将返回 true
2.如果用户在过去拒绝了权限请求,并在权限请求系统对话框中选择了 Don’t ask again 选项,此方法将返回 false
用户之前已经拒绝了,但是该应用需要某种权限,需要提示用户去选择

ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)
4、权限申请回调
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 123){
        	// 权限拒绝
            if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
                if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.CAMERA)){
                    // 点击了不再询问弹窗    
                    Log.e("权限", "不再询问处理....." );
                }else {
                	// 普通的拒绝
                    finish();
                }
            }else {
            	// 同意权限申请的分支
                detailInAction();
            }
        }
    }
5、跳转到该应用的设置界面
    /**
     * 跳转到设置权限的界面
     * */
    public void getAppDetailSettingIntent() {
        // 可以看下这篇文章
        // https://blog.csdn.net/cbbbc/article/details/60148864
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            intent.setAction(Intent.ACTION_VIEW);
            intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            intent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }
        startActivity(intent);
    }
完整的例子
/**
 * @author crazyZhangxl on 2018-11-16 8:15:09.
 * Describe: 闪屏页
 */

public class SplashActivity extends BaseActivity   {
    @Override
    protected void init() {

    }
    @Override
    protected int provideContentViewId() {
        return 0;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        if (checkAndRequestPermission()){
            detailInAction();
        }
    }

    private void detailInAction(){
        // 进行判断进行页面跳转
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

        if (TextUtils.isEmpty(UserCache.getAuthorization())){
            LoginActivity.show(SplashActivity.this);
        }else {
            IndexActivity.show(SplashActivity.this);
        }
        // 再关闭啊
        finish();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("权限", "onResume: ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("权限", "onRestart: ");
        if (checkAndRequestPermission()){
            detailInAction();
        }
    }

    private boolean checkAndRequestPermission(){
        int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
        // "不再询问"不会进入该循环,勾选该表示已经"授权"
        if (permission != PackageManager.PERMISSION_GRANTED){
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)){
                // 用户拒绝过 但是未勾选不再询问
                showPSMaterialDialog(null, "需要开启大桥对您手机的相机权限才能下载安装,是否现在开启", "确定", "取消",
                        new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                dialog.dismiss();
                                ActivityCompat.requestPermissions(SplashActivity.this,new String[]{Manifest.permission.CAMERA},123);
                            }
                        }, new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                dialog.dismiss();
                                finish();
                            }
                        });

            }else {
                // 首次检查权限
                // 如果不再询问 不会执行...
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},123);
            }
            return false;
        }
        return true;
    }

    // onResume
    // onRequestPermissionsResult
    // onResume
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.e("权限", "onRequestPermissionsResult: " );
        if (requestCode == 123){
            if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
                // 点击了不再询问 返回的结果为false.... =>
                // 由于用户点击了不再询问且拒绝  需要用户手动去用户设置里开启
                if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.CAMERA)){
                    // 弹窗 提示用户去设置
                    showPSMaterialDialog(null,
                            "使用应用前需要获取您的相机的使用权限。\n操作路径:设置->应用->延长->权限", "去设置", "取消",
                            (dialog, which) -> {
                                dialog.dismiss();
                                // 跳转向应用设置页面
                                SplashActivity.this.getAppDetailSettingIntent();
                            }, (dialog, which) -> {
                                dialog.dismiss();
                                SplashActivity.this.finish();
                            });
                }else {
                    finish();
                }
            }else {
                detailInAction();
            }
        }
    }
}
Android  权限相关Android  权限相关 奔跑的杰尼龟 发布了97 篇原创文章 · 获赞 37 · 访问量 7万+ 私信 关注
上一篇:Android java.lang.NoClassDefFoundError:failed resolution of :Lorg/apache/http/ProtocolVersion


下一篇:移动APP开发使用