如何在新的 ActivityResult API (1.3.0-alpha05) 中获取权限请求?
Posted
技术标签:
【中文标题】如何在新的 ActivityResult API (1.3.0-alpha05) 中获取权限请求?【英文标题】:How to get a permission request in new ActivityResult API (1.3.0-alpha05)? 【发布时间】:2020-09-23 21:36:50 【问题描述】:所以,我尝试使用新的 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
【问题讨论】:
【参考方案1】:更新 这个答案有效,但我找到了一个更好的权限请求解决方案,没有空洞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
中声明?
【讨论】:
权限被拒绝和带有“不再询问”选项的情况如何??? 有什么方法可以检查用户是否永久拒绝?显示相关消息。 @VishakAKamath 这个答案是从链接的文档中复制的。在练习了更多之后,我想出了一种更简洁、更好的权限请求流程方法。查看this 以获取您所有问题的答案。 @MarlonLópez 检查我上面链接的答案或here【参考方案2】:看着Update to androidx.fragment:fragment:1.3.0-alpha08: registerForActivityResult not allowed after onCreate anymore. How to use after onCreate?,
private lateinit var checkLocationPermission: ActivityResultLauncher<Array<String>>
// Initialize checkLocationPermission in onAttach or onCreate.
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
checkLocationPermission = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()) permissions ->
if (permissions[Manifest.permission.ACCESS_FINE_LOCATION] == true ||
permissions[Manifest.permission.ACCESS_COARSE_LOCATION] == true)
initUserLocation()
else
// Permission was denied. Display an error message.
fun showMap()
if (ActivityCompat.checkSelfPermission(requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(requireContext(),
Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
initUserLocation()
else
checkLocationPermission.launch(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION))
private fun initUserLocation()
googleMap?.isMyLocationEnabled = true
【讨论】:
以上是关于如何在新的 ActivityResult API (1.3.0-alpha05) 中获取权限请求?的主要内容,如果未能解决你的问题,请参考以下文章
如何在新的 Spotify Web api 上获取用户的关注者和用户的关注用户?
如何在新的 Google Play 控制台中停用所有旧版本的 APK?
onActivityResult() 未在新的嵌套片段 API 中调用