简体   繁体   English

isExternalStorageManager() = true 在 Android 11 之后的不一致行为

[英]Inconsistent behavior after isExternalStorageManager() = true in Android 11

I added a Preference button in my PreferenceScreen to call for Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION in Android 11:我在我的 PreferenceScreen 中添加了一个 Preference 按钮来调用 Android 11 中的Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION

        prefn.setOnPreferenceClickListener(arg0 -> {
            Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            Uri uri = Uri.fromParts("package", getPackageName(), null);
            intent.setData(uri);
            startActivity(intent);
            return true;
        });

        if (Environment.isExternalStorageManager()) {
            pref.setSummary("Allowed");
        } else {
            pref.setSummary("Not Allowed");
        }

When I change the setting from Allow to Not allow and the setting screen is closed, the PreferenceScreen is reloaded (in particular, the onCreate() ), and the pref summary changes to "Not Allowed", as expected.当我将设置从Allow更改为Not allow并关闭设置屏幕时,将重新加载 PreferenceScreen(特别是onCreate() ),并且首选项摘要更改为“不允许”,如预期的那样。 However, if I click and change the setting from Not allow to Allow , it is not reloaded, and the summary still shows "Not allowed" despite the fact that the setting is in the Allow state.但是,如果我单击并将设置从Not allow更改为Allow ,它不会重新加载,并且摘要仍然显示“不允许”,尽管该设置位于Allow state 中。 Even stranger, if I change 3 times, Not allow to Allow to Not allow to Allow , it is reloaded and the summary shows "Allowed", as it should.更奇怪的是,如果我更改 3 次,将Not allow to Allow更改为Not allow to Allow ,它会重新加载,并且摘要显示“Allowed”,应该如此。

Any clue??有什么线索吗?? I tried calling startActivityForResult without success.我尝试调用startActivityForResult没有成功。

This is not a solution, but a workaround.这不是解决方案,而是一种解决方法。 And a coward one, sorry.一个懦夫,对不起。

Simply fix the thing through the onResume() method of the Preference activity.只需通过 Preference 活动的onResume()方法来解决问题。 Namely, before calling Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION , set a variable:即,在调用Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION之前,设置一个变量:

lastManagerPerm = Environment.isExternalStorageManager();

and then in the onResume() :然后在onResume()

public void onResume() {
    super.onResume();
    if (Build.VERSION.SDK_INT >= 30 && !lastManagerPerm && Environment.isExternalStorageManager()) {
         prefn.setSummary("Allowed");
         ....
    }
}

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

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