ActivityResultLauncher with RequestMultiplePermissions contract doesn#39;t show permissions UI on launch(ActivityResultLauncher与RequestMultiplePermission合同在启动时不显示权限用户界面)
问题描述
我正在尝试使用ActivityResultLauncher
和ActivityResultsContract.RequestMultiplePermissions
请求权限。
public class MainActivity extends AppCompatActivity {
final String[] PERMISSIONS = {
Manifest.permission.FOREGROUND_SERVICE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION,
Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
};
private ActivityResultContracts.RequestMultiplePermissions requestMultiplePermissionsContract;
private ActivityResultLauncher<String[]> multiplePermissionActivityResultLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestMultiplePermissionsContract = new ActivityResultContracts.RequestMultiplePermissions();
multiplePermissionActivityResultLauncher = registerForActivityResult(requestMultiplePermissionsContract, isGranted -> {
Log.d("PERMISSIONS", "Launcher result: " + isGranted.toString());
if (isGranted.containsValue(false)) {
Log.d("PERMISSIONS", "At least one of the permissions was not granted, launching again...");
multiplePermissionActivityResultLauncher.launch(PERMISSIONS);
}
});
askPermissions(PERMISSIONS);
}
private void askPermissions() {
if (!hasPermissions(PERMISSIONS)) {
Log.d("PERMISSIONS", "Launching multiple contract permission launcher for ALL required permissions");
multiplePermissionActivityResultLauncher.launch(PERMISSIONS);
} else {
Log.d("PERMISSIONS", "All permissions are already granted");
}
}
private boolean hasPermissions(String[] permissions) {
if (permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
Log.d("PERMISSIONS", "Permission is not granted: " + permission);
return false;
}
Log.d("PERMISSIONS", "Permission already granted: " + permission);
}
return true;
}
return false;
}
}
我还在build.gradle
中包括了必要的依赖项:
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.activity:activity:1.2.0'
implementation 'androidx.fragment:fragment:1.3.0'
当我在设备上启动应用程序时,没有显示允许权限的UI。在启动之前,我清除了缓存并从设备上删除了应用程序。 日志:
2021-03-04 15:02:43.584 28476-28476/com.example.app D/PERMISSIONS: Permission already granted: android.permission.FOREGROUND_SERVICE
2021-03-04 15:02:43.586 28476-28476/com.example.app D/PERMISSIONS: Permission is not granted: android.permission.ACCESS_FINE_LOCATION
2021-03-04 15:02:43.586 28476-28476/com.example.app D/PERMISSIONS: Launching multiple contract permission launcher for ALL required permissions
2021-03-04 15:02:43.753 28476-28476/com.example.app D/PERMISSIONS: Launcher result: {android.permission.ACCESS_FINE_LOCATION=false, android.permission.ACCESS_BACKGROUND_LOCATION=false}
2021-03-04 15:02:43.753 28476-28476/com.example.app D/PERMISSIONS: At least one of the permissions was not granted, launching again...
2021-03-04 15:02:43.857 28476-28476/com.example.app D/PERMISSIONS: Launcher result: {android.permission.ACCESS_FINE_LOCATION=false, android.permission.ACCESS_BACKGROUND_LOCATION=false}
2021-03-04 15:02:43.857 28476-28476/com.example.app D/PERMISSIONS: At least one of the permissions was not granted, launching again...
2021-03-04 15:02:43.939 28476-28476/com.example.app D/PERMISSIONS: Launcher result: {android.permission.ACCESS_FINE_LOCATION=false, android.permission.ACCESS_BACKGROUND_LOCATION=false}
最后两行无限重复,直到我关闭应用程序。
但是,当我使用ActivityResultContracts.RequestPermission
并分别请求每个权限时,它会按预期工作,并显示所有必要的用户界面。
为什么ActivityResultsContract.RequestMultiplePermissions
对我的案例无效?
推荐答案
我找到了问题的答案。事实证明,在Android 11ACCESS_BACKGROUND_LOCATION
中,不应该与其他权限一起请求权限。如果是,系统将直接忽略它。应单独请求此权限。
此外,如果用户在第一次请求时决定不授予此权限,则将忽略以下对此权限的所有请求,因此不会显示该权限的设置页面(在这种情况下,您可能应该提示用户手动允许该权限)。
这篇关于ActivityResultLauncher与RequestMultiplePermission合同在启动时不显示权限用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ActivityResultLauncher与RequestMultiplePermission合同在启动时不显示权限用户界面
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01