MANAGE_EXTERNAL_STORAGE is not calling registerForActivityRe



我正在将应用程序升级到目标android 12,然后再升级到目标android 10。因此,现在我使用MANAGE_EXTERNAL_STORAGEregisterForActivityResult,但当用户允许访问管理文件并按下后退按钮时,不会调用registerForActivityResult。这是我的密码
onCreate方法中的

registerLauncher();
if (checkPermissionsGranted()) {
initMainActivity();
} else {
requestPermission();
}

以上方法按顺序排列。

private void registerLauncher() {
permissionLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Log.e(TAG, "inside activity result");
initMainActivity();
}
}
});
}
private boolean checkPermissionsGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return Environment.isExternalStorageManager();
} else {
int readPermission = ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE);
int writePermission = ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE);
//            int storagePermission = ContextCompat.checkSelfPermission(mContext, Manifest.permission_group.STORAGE);
return readPermission == PackageManager.PERMISSION_GRANTED && writePermission == PackageManager.PERMISSION_GRANTED;
}
}
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
try {
//                Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
//                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri);
//                startActivity(intent);
//                permissionLauncher.launch(intent);
//                startActivityForResult(intent, AppConstants.STORAGE_PERMISSION_CODE);
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", getApplicationContext().getPackageName())));
//   startActivityForResult(intent, AppConstants.STORAGE_PERMISSION_CODE);
permissionLauncher.launch(intent);
} catch (Exception e) {
Log.e(TAG, "Exp... " + e.toString());
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
permissionLauncher.launch(intent);
}
} else {
ActivityCompat.requestPermissions(Splashscreen.this, new String[]{Manifest.permission_group.STORAGE}, AppConstants.STORAGE_PERMISSION_CODE);
}
}

我也尝试过用startActivityForResult(intent, AppConstants.STORAGE_PERMISSION_CODE);(我知道startActivityForResult不推荐使用(重写onRequestPermissionsResult,但这两个都没有被调用
1-registerForActivityResult
2-onRequestPermissionsResult

请告诉我我在这里做错了什么?当我检查splash活动的权限时,由于没有调用onResult,应用程序被卡住了。

谢谢&问候

在检查了splash活动的权限后,您有两个返回应用程序的选项。点击左上角的后退按钮或点击应用程序图标。但您将永远不会得到Activity.RESULT_OK作为结果。因此,您应该通过查询Environment.isExternalStorageManager((.来进行不同的处理

更改registerLauncher((函数如下。

private void registerLauncher() {
permissionLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (Environment.isExternalStorageManager()) {
Log.e(TAG, "inside activity result");
initMainActivity();
}
}
});
}

相关内容

  • 没有找到相关文章

最新更新