gpt4 book ai didi

java - 如何在新的 ActivityResult API (1.3.0-alpha05) 中获取权限请求?

转载 作者:行者123 更新时间:2023-12-04 04:28:39 24 4
gpt4 key购买 nike

因此,我尝试使用新的 registerForActivityResult() 方法获得许可,并通过 .launch() 按钮单击来请求它,它似乎没有打开任何窗口来请求它。
我总是在 registerForActivityResult() 中得到错误。

    // Permission to get photo from gallery, gets permission and produce boolean
private ActivityResultLauncher<String> mPermissionResult = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
new ActivityResultCallback<Boolean>() {
@Override
public void onActivityResult(Boolean result) {
if(result) {
Log.e(TAG, "onActivityResult: PERMISSION GRANTED");
} else {
Log.e(TAG, "onActivityResult: PERMISSION DENIED");
}
}
});



// Launch the permission window -- this is in onCreateView()
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPermissionResult.launch(Manifest.permission.ACCESS_BACKGROUND_LOCATION);

}
});

这是我的日志:onActivityResult: PERMISSION DENIED

最佳答案

更新
这个答案有效,但我找到了一个更好的解决方案来解决没有开放漏洞的权限请求 here .

来自 docs :
在您的 Activity/fragment 中,创建此字段:

// Register the permissions callback, which handles the user's response to the
// system permissions dialog. Save the return value, an instance of
// ActivityResultLauncher, as an instance variable.
private ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new RequestPermission(), isGranted -> {
if (isGranted) {
// Permission is granted. Continue the action or workflow in your
// app.
} else {
// Explain to the user that the feature is unavailable because the
// features requires a permission that the user has denied. At the
// same time, respect the user's decision. Don't link to system
// settings in an effort to convince the user to change their
// decision.
}
});
在同一个 Activity/fragment 中的某处:
if (ContextCompat.checkSelfPermission(
context, Manifest.permission.ACCESS_BACKGROUND_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
performAction(...);
} else if (shouldShowRequestPermissionRationale(...)) {
// In an educational UI, explain to the user why your app requires this
// permission for a specific feature to behave as expected. In this UI,
// include a "cancel" or "no thanks" button that allows the user to
// continue using your app without granting the permission.
showInContextUI(...);
} else {
// You can directly ask for the permission.
// The registered ActivityResultCallback gets the result of this request.
requestPermissionLauncher.launch(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
}
如果您一直收到不合理的“Permission denied”,可能是您没有在 manifest.xml 中声明它。 ?

关于java - 如何在新的 ActivityResult API (1.3.0-alpha05) 中获取权限请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62202471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com