Permission Requests causes infinite loop in OnResume

前端 未结 3 1933
春和景丽
春和景丽 2021-01-17 12:21

In API >= 23, we are required to ask users for permission at run-time. But for some reason, the permissions are causing onResume to be called infinitely. What causes this?

3条回答
  •  被撕碎了的回忆
    2021-01-17 12:49

    A small piece of code for permissions to complete previous response :)

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        if (Build.VERSION.SDK_INT >= 23)
            ensurePermissions(
                    Manifest.permission.GET_ACCOUNTS,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            );
    }
    

    and:

    @TargetApi(23)
    private void ensurePermissions(String... permissions) {
        boolean request = false;
        for (String permission : permissions)
            if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                request = true;
                break;
            }
    
        if (request) {
            requestPermissions(permissions, REQUEST_CODE_PERMISSION);
        }
    }
    

提交回复
热议问题