ActivityResultLauncher与RequestMultiplePermission合同在启动时不显示权限用户界面

ActivityResultLauncher with RequestMultiplePermissions contract doesn#39;t show permissions UI on launch(ActivityResultLauncher与RequestMultiplePermission合同在启动时不显示权限用户界面)

本文介绍了ActivityResultLauncher与RequestMultiplePermission合同在启动时不显示权限用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ActivityResultLauncherActivityResultsContract.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合同在启动时不显示权限用户界面

基础教程推荐