在android中不请求许可,程序直接遇到拒绝块

问题描述 投票:0回答:2

嗨,最近我开始在 android studio 中编码,我添加了 dexter 来获取访问用户存储的权限,但主要担心的是,即使不请求权限,代码也会直接显示拒绝阻止的消息。请帮忙

Dexter.withContext(this)
                    .withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
    
                    .withListener(new PermissionListener() {
                        @Override
                        public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
                            Toast.makeText(MainActivity.this, "Thanks !", Toast.LENGTH_SHORT).show();
                        }
    
                        @Override
                        public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
                            Toast.makeText(MainActivity.this, "not granted", Toast.LENGTH_SHORT).show();
                        }
    
                        @Override
                        public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
                            permissionToken.continuePermissionRequest();
                        }
                    })
                    .check();
java android android-external-storage
2个回答
1
投票

可能是您的应用程序还没有权限。
从 Android 6 开始,某些权限被视为“危险权限”,即使您将所需的权限添加到 Android 清单中,您也不应该假设您的应用程序已经拥有它,并且您应该始终在运行时请求权限,然后再继续执行操作需要它们。

如果您没有将
    READ_EXTERNAL_STORAGE
  • 添加到 Android 清单中,它应该如下所示:
    
    
  • <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your.package.name" <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application> ... </application> </manifest>
如果您确实添加了它,但对外部存储的访问仍然被拒绝,那么这是在运行时请求它的方法:
    首先初始化一个
  1. ActivityResultLauncher

    并将其注册为活动结果,必须在{初始化、

    onAttach()
    onCreate()
    }片段创建方法之前,所以唯一的方法是将其初始化为全局片段变量
    
    

  2. 然后当您需要开始使用外部存储时,您首先使用之前初始化的
  3. ActivityResultLauncher

    来发起所需权限的请求。

    
    

  4. 如果授予权限,回调应该自然地继续您的工作流程,否则(暂时)您应该显示一个 toast/Alert 对话框消息,告诉用户为什么他不能继续他的操作。
  5. public class myFragment extends Fragment { ... ActivityResultLauncher<String> requestPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> { if (isGranted){ //continue your work flow goDoSomething();} else{ // Explain to the user that the feature is unavailable because // the features requires a permission that the user has denied. // At the same time, respect the user's decision. Don't link to // system settings in an effort to convince the user to change // their decision. Toast.makeText(getContext(), "Can't continue without the required permissions", Toast.LENGTH_LONG).show(); } }); public void getNecessaryPermissionsAndDoSomething() { if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { //check first if you have the permission or not //if you don't then launch permission request dialog requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE); }else goDoSomething(); //continue your work naturally } }
有关此内容的更多信息,您应该查看
android开发人员文档

有关在运行时请求权限的信息。

更新

if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) Log.e("permission","not granted"); else Log.e("permission","granted");



0
投票

<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-feature android:name="android.hardware.camera.any" /> <uses-permission android:name="android.permission.CAMERA" />

© www.soinside.com 2019 - 2024. All rights reserved.