如何知道该应用当前是否具有所有文件访问'是否许可。 Android 11

发布于 2025-02-11 08:06:35 字数 381 浏览 1 评论 0 原文

但是如何验证该应用当前是否已拥有“所有文件访问”权限。我使用此方法询问许可。

Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setData(Uri.parse(String.format("package:%s",this.getPackageName())));
            startActivityForResult(intent, 2296);

请帮助我解决这个问题:(

but how to validate if the app currently has the 'All Files Access' permission or not. i use this method to ask permission.

Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setData(Uri.parse(String.format("package:%s",this.getPackageName())));
            startActivityForResult(intent, 2296);

Please help me to fix this :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾`听者〃 2025-02-18 08:06:36

在内部内存的根部创建新的虚拟0个bytes文件,然后将其删除。如果两个操作成功,那么您就可以访问。当您无法访问时,您的代码将投掷异常(在创建文件中),您可以尝试{} catch(exception e)(防止应用应用程序崩溃)

内部/外部/共享的路径可以从 环境 班级,我首先尝试 /代码>

create new dummy 0-bytes file in root of internal memory and then remove it. if both operations success then you have access. when you won't have access then your code will throw Exception (on create-file step), which you can try{ }catch(Exception e) (preventing app crash)

path for internal/external/shared can be obtained from Environment class, I would try at first getExternalStorageDirectory

jJeQQOZ5 2025-02-18 08:06:36

我这样做:

 public static boolean hasAllFilesAccess(Context context) {

    AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);

    try {
        ApplicationInfo app  = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
        return appOpsManager.unsafeCheckOpNoThrow("android:manage_external_storage",app.uid, context.getPackageName()) == AppOpsManager.MODE_ALLOWED;

    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    catch (NoSuchMethodError e)
    {
        return true;
    }

    return false;
}

似乎现在有一种更好的方法:

I do it that way:

 public static boolean hasAllFilesAccess(Context context) {

    AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);

    try {
        ApplicationInfo app  = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
        return appOpsManager.unsafeCheckOpNoThrow("android:manage_external_storage",app.uid, context.getPackageName()) == AppOpsManager.MODE_ALLOWED;

    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    catch (NoSuchMethodError e)
    {
        return true;
    }

    return false;
}

Seems there is a better way now though:

Environment.isExternalStorageManager()

伤感在游骋 2025-02-18 08:06:35

在您的 subtest.xml 中声明此权限:

   <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

要询问 manage_external_storage

      try {
           Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
           intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
           startActivityForResult(intent, 2296);
          
       } catch (Exception e) {

         Intent intent = new Intent();
         intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
         startActivityForResult(intent, 2296);
       }

onActivityResult 中获取结果

   @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2296) {
            if (SDK_INT >= Build.VERSION_CODES.R) {
                if (Environment.isExternalStorageManager()) {
                    // perform action when allow permission success
                   
                } else {
                    Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

在 在这样的android11中;

if (SDK_INT >= Build.VERSION_CODES.R) {
   if (Environment.isExternalStorageManager()) {
      // Permission Granted
   }
}

declare this permission in your manifest.xml:

   <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

for asking MANAGE_EXTERNAL_STORAGE:

      try {
           Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
           intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
           startActivityForResult(intent, 2296);
          
       } catch (Exception e) {

         Intent intent = new Intent();
         intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
         startActivityForResult(intent, 2296);
       }

get your result in onActivityResult

   @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2296) {
            if (SDK_INT >= Build.VERSION_CODES.R) {
                if (Environment.isExternalStorageManager()) {
                    // perform action when allow permission success
                   
                } else {
                    Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

so, basically you can check permission is granted or not in Android11 like this;

if (SDK_INT >= Build.VERSION_CODES.R) {
   if (Environment.isExternalStorageManager()) {
      // Permission Granted
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文