android - 如何在 jetpack compose 中观察 onActivityResult()?
问题描述
我正在尝试Change location settings
从这里启用 gps 设置:https ://developer.android.com/training/location/change-location-settings
它说check the result in onActivityResult()
,但我不知道如何在 jetpack compose 中观察结果。请告诉我如何在撰写中观察此结果
task.addOnFailureListener { exception ->
if (exception is ResolvableApiException){
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
exception.startResolutionForResult(this@MainActivity,
REQUEST_CHECK_SETTINGS)
} catch (sendEx: IntentSender.SendIntentException) {
// Ignore the error.
}
}
}
解决方案
如果有人需要答案
val settingResultRequest =
rememberLauncherForActivityResult(contract = ActivityResultContracts.StartIntentSenderForResult()) {activityResult->
if (activityResult.resultCode == RESULT_OK)
Log.d("appDebug", "Accepted")
else {
Log.d("appDebug", "Denied")
}
}
task.addOnFailureListener { exception ->
if (exception is ResolvableApiException) {
try {
val intentSenderRequest = IntentSenderRequest.Builder(exception.resolution).build()
settingResultRequest.launch(intentSenderRequest)
} catch (sendEx: IntentSender.SendIntentException) {
// Ignore the error.
}
}
}
推荐阅读
- c# - SQLite:设计数据库并使用 AUTOINCREMENT ID 访问其数据的正确方法?
- database - 未在 Android Studio 中创建表
- django - 显示 POST 值的 ['on'] 的 Django 单选表单
- javascript - 单击按钮时从右向左滑动隐藏的引导卡
- c - 创建动态线程数时出错
- php - Shell_exec 不使用 POST 变量
- performance - Jmeter:控制一次发生的请求数
- sql-server - 为什么我在编写存储过程时收到错误 8101?
- macos - Mac OS:泄漏消毒剂
- java - 二叉树的层序遍历(问题的具体细节参考下面的代码)