Android 申请权限,安卓11、安卓12文件权限,RxPermissions使用,android.permission.WRITE_SETTINGS

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            ActivityCompat.requestPermissions(
//                    MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
                    MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);

            return;
        }

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {//安卓11文件权限
    // 先判断有没有权限
    if (Environment.isExternalStorageManager()) {//已有权限-----------------
        onHeadClick();
    } else {
        Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
        return;
    }
}

12------------------------------------------------------------------------------

final Intent enableIntent2 = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent2, BLUE_REQUEST_CODE);
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

 

  ActivityCompat.requestPermissions(
                MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

rootPath = DocumentsUtils.getStoragePath(UserInfoActivity.this, false);  //获取 可以插拔的 sd 卡  路径
LgqLogPlus.e("SD卡路径-=----- "+rootPath);
if (DocumentsUtils.checkWritableRootPath(UserInfoActivity.this, rootPath)) {   //检查sd卡路径是否有 权限 没有显示dialog
    String fileok12= SharedPreUtil.getString(mContext,"shierfileok","no");
    if (fileok12.equals("ok")){
        onHeadClick();

    }else {
        if (android.os.Build.VERSION.SDK_INT >= 31) {
            showOpenDocumentTree();

        }else {
            onHeadClick();

        }

    }
}else {
    onHeadClick();

}
    String   rootPath ;

    private void showOpenDocumentTree() {
//        String fileok12= SharedPreUtil.getString(mContext,"shierfileok","no");
//        if (fileok12.equals("ok")){
//            return;
//        }
        Intent intent = null;
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        if (android.os.Build.VERSION.SDK_INT >= 31) {
            StorageManager sm = getSystemService(StorageManager.class);
            StorageVolume volume = sm.getStorageVolume(new File(rootPath));
            if (volume != null) {
                intent = volume.createAccessIntent(null);
            }
        }
        if (intent == null) {
            intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        }
        startActivityForResult(intent, DocumentsUtils.OPEN_DOCUMENT_TREE_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("lgq","lgqrequ==== " + requestCode + ".....res===" + resultCode + "......" + data);
        switch (requestCode) {
            case DocumentsUtils.OPEN_DOCUMENT_TREE_CODE:
                if (resultCode==RESULT_OK){
//                    SharedPreUtil.putString("shierfileok","ok");
                }
                if (data != null && data.getData() != null) {
                    Uri uri = data.getData();
                    DocumentsUtils.saveTreeUri(this, rootPath, uri);

                }

                break;
        }
    }

if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
    // TODO: Consider calling
    //    ActivityCompat#requestPermissions
    // here to request the missing permissions, and then overriding
    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
    //                                          int[] grantResults)
    // to handle the case where the user grants the permission. See the documentation
    // for ActivityCompat#requestPermissions for more details.
    return 1;
}

1.需要用户手动赋予的权限( Dangerous Permissions)

所属权限组权限
日历READ_CALENDAR
日历WRITE_CALENDAR
相机CAMERA
联系人READ_CONTACTS
联系人WRITE_CONTACTS
联系人GET_ACCOUNTS
位置ACCESS_FINE_LOCATION
位置ACCESS_COARSE_LOCATION
麦克风RECORD_AUDIO
电话READ_PHONE_STATE
电话CALL_PHONE
电话READ_CALL_LOG
电话WRITE_CALL_LOG
电话ADD_VOICEMAIL
电话USE_SIP
电话PROCESS_OUTGOING_CALLS
传感器BODY_SENSORS
短信SEND_SMS
短信RECEIVE_SMS
短信READ_SMS
短信RECEIVE_WAP_PUSH
短信RECEIVE_MMS
存储READ_EXTERNAL_STORAGE
存储WRITE_EXTERNAL_STORAGE

还有指纹识别权限:

<uses-permission android:name="android.permission.USE_FINGERPRINT" />
注意:如果应用程序请求在AndroidManifest中列出的危险权限,并且应用程序已经在同一权限组中具有另一个危险权限,系统会立即授予权限,而不会与用户进行任何交互,。例如,如果一个应用程序先前已经请求并被授予READ_CONTACTS权限,然后它请求WRITE_CONTACTS(同属于联系人一组),系统会立即授予该权限,不会再弹出权限授予询问的对话框。

3、实现方法2——简便实现 RxPermissions 的使用

rxPermissions.requestEach(strPer)
        .subscribe(new Consumer<Permission>() {
            @Override
            public void accept(Permission permission) {
                if (permission.granted) {
                    grantedCount++;
                    if (grantedCount == strPer.length){
                        releaseCamera();
                        camera = Camera.open(1);
                        camera.setDisplayOrientation(90);
                        Log.e("lgq","同意000");
                        try {
                            camera.setPreviewDisplay(surfaceView.getHolder());
                            camera.startPreview();
                        } catch (IOException e) {
                            Log.e(TAG, "Error while trying to display the camera preview", e);
                        }
                    }

                } else if (permission.shouldShowRequestPermissionRationale) {
                    // Denied permission without ask never again
                    Toast.makeText(MainActivity.this,
                            "Denied permission without ask never again",
                            Toast.LENGTH_SHORT).show();
                    Log.e("lgq","拒绝111");
                } else {
                    // Denied permission with ask never again
                    // Need to go to the settings
                    Toast.makeText(MainActivity.this,
                            "Permission denied, can't enable the camera",
                            Toast.LENGTH_SHORT).show();
                    Log.e("lgq","取消1112222多次拒绝");
                }
            }
        });

  int grantedLocationCount = 0;
    private void checkPermission() {

        final String strPer[] = {Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION
        };
        grantedLocationCount = 0;
        final RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(strPer)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) {
                        if (permission.granted) {
                            grantedLocationCount++;
                            if (grantedLocationCount == strPer.length) {
                                //请求成功操作
                                isWifi5G();


                            }
                        } else if (permission.shouldShowRequestPermissionRationale) {
//                            ShowPermissionDialog(LangComm.getString("need_permissions"));
                        } else {
//                            ShowPermissionDialog(LangComm.getString("android_check_permissions"));
                        }
                    }
                });


    }

 demo连接:https://download.csdn.net/download/meixi_android/86519919

android.permission.WRITE_SETTINGS 申请

Context context = getApplicationContext();

// Check whether has the write settings permission or not.
boolean settingsCanWrite = Settings.System.canWrite(context);

if(!settingsCanWrite) {
    // If do not have write settings permission then open the Can modify system settings panel.
    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
    startActivity(intent);
}else {
    // If has permission then show an alert dialog with message.
    AlertDialog alertDialog = new AlertDialog.Builder(LinyuMainFragmentActy.this).create();
    alertDialog.setMessage("You have system write settings permission now.");
    alertDialog.show();
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值