当前位置 : 主页 > 编程语言 > c++ >

Android 6.0 权限处理

来源:互联网 收集:自由互联 发布时间:2021-07-03
Android 6.0 之后,一些权限需要二次申请,包括相机,联系人,内存卡,短信等等。 if (Build.VERSION.SDK_INT = 23) { int checkCallPhonePermission = ContextCompat.checkSelfPermission(StartActivity.this, Manifest.per
Android 6.0 之后,一些权限需要二次申请,包括相机,联系人,内存卡,短信等等。
if (Build.VERSION.SDK_INT >= 23) {
                    int checkCallPhonePermission = ContextCompat.checkSelfPermission(StartActivity.this, Manifest.permission.CAMERA);
                    int permissionWrite = ActivityCompat.checkSelfPermission(StartActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                    if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED || permissionWrite != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(StartActivity.this,
                                new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, 777);
                    } else {
                        //已经获取该权限时的处理
                        JumpOther();
                    }
                } else {
                        //Android版本低于6.0时的处理
                    JumpOther();
                }

    
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            //就像onActivityResult一样这个地方就是判断你是从哪来的。
            case 777:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //权限获取成功
                    JumpOther();
                } else {
                    CustomToast.showToast(this, "请务必打开权限。");
                    finish();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
网友评论