简体   繁体   English

Android 如何拒绝已经存在的权限

[英]Android how to deny the permission which is already

Here is my java code这是我的 java 代码

 cameraSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                    if(isChecked){
                        strName="Camera";
                        checkAndRequestPermissions();
                    }                  }
                }
            });
 public  void checkAndRequestPermissions(){
            int camera = ContextCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA);
            List<String> listPermissionsNeeded = new ArrayList<String>();
            if(strName.equals("Camera")){
                if (camera != PackageManager.PERMISSION_GRANTED) {
                    listPermissionsNeeded.add(Manifest.permission.CAMERA);
                }
            }
            if (!listPermissionsNeeded.isEmpty()) {
                ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
            }
    }

      @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_ID_MULTIPLE_PERMISSIONS: {
                Map<String, Integer> perms = new HashMap<>();
                perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
                if (grantResults.length > 0) {
                    for (int i = 0; i < permissions.length; i++)
                        perms.put(permissions[i], grantResults[i]);
                    if(strName.equals("Camera")){
                        if (perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
                            cameraSwitch.setOnCheckedChangeListener(null);
                            cameraSwitch.setChecked(true);
                        }else if (perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED) {
                            cameraSwitch.setChecked(false);
                        }
                    }
                }
            }
        }
    }

I am implementing run time permissions in my android application.i have switch button if i click the switch button i am granting camera permission similarly i want to deny the permission when switch button becomes deactive.please help me to deny the permission when switch button is deactivated.我在我的 android 应用程序中实现运行时权限。如果我单击切换按钮,我有切换按钮我正在授予相机权限同样我想在切换按钮变为非活动状态时拒绝权限。请帮助我在切换按钮时拒绝权限停用。

i want to deny the permission when switch button becomes deactive.please help me to deny the permission when switch button is deactivated.我想在开关按钮停用时拒绝权限。请帮助我在开关按钮停用时拒绝权限。

there is no way to remove the gran programmatically.无法以编程方式删除 gra。 What you can do is to forward the user to your app's setting screen.您可以做的是将用户转发到您的应用程序的设置屏幕。 There the user can revoke the permission在那里,用户可以撤销权限

Try this code that opens the settings screen of the app, so the user will be able to deny the permissions from there:试试这个打开应用程序设置屏幕的代码,这样用户就可以从那里拒绝权限:

Intent i = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
i.setData(uri);
startActivity(i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM