在 InAppUpdates 中获取 startUpdateFlowForResult 回调而不是 onActivityResult 的替代方法是什么,因为它已被弃用?
我们必须等待 Google Play 团队迁移出已弃用的 API。您可以在 Google 的 Issue Tracker 上关注此问题。
创建此结果启动器
private val updateFlowResultLauncher =
registerForActivityResult(
ActivityResultContracts.StartIntentSenderForResult(),
) { result ->
if (result.resultCode == RESULT_OK) {
// Handle successful app update
}
}
之后尝试像这样启动你的意图
val starter =
IntentSenderForResultStarter { intent, _, fillInIntent, flagsMask, flagsValues, _, _ ->
val request = IntentSenderRequest.Builder(intent)
.setFillInIntent(fillInIntent)
.setFlags(flagsValues, flagsMask)
.build()
updateFlowResultLauncher.launch(request)
}
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.FLEXIBLE,
starter,
requestCode,
)
尝试一下!
已提供更新的 Google 文档: https://developer.android.com/guide/playcore/in-app-updates/kotlin-java
您可以像这样启动更新:
appUpdateManager.startUpdateFlowForResult(
// Pass the intent that is returned by 'getAppUpdateInfo()'.
appUpdateInfo,
// an activity result launcher registered via registerForActivityResult
activityResultLauncher,
// Or pass 'AppUpdateType.FLEXIBLE' to newBuilder() for
// flexible updates.
AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build())
并且
activityResultLauncher
在您的 Activity 中设置如下:
private val activityResultLauncher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
val resultCode = result.resultCode
when {
resultCode == Activity.RESULT_OK -> {
Log.v("MyActivity", "Update flow completed!")
}
resultCode == Activity.RESULT_CANCELED -> {
Log.v("MyActivity", "User cancelled Update flow!")
}
resultCode != ActivityResult.RESULT_IN_APP_UPDATE_FAILED -> {
Log.v("MyActivity", "Update flow failed!")
}
}
}
您可以使用下面的代码片段替代 onActivityResult() 第一个活动
步骤1
private val openActivity =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
handleActivityResult(REQUEST_CODE, it)
}
第2步
openActivity.launch(
Intent(this, YourClass::class.java).apply {
putExtra(ANY_KEY, data) // If any data you want to pass
}
)
第3步
private fun handleActivityResult(requestCode: Int, result: ActivityResult?) {
Timber.e("========***handleActivityResult==requestActivty==$requestCode====resultCode=========${result?.resultCode}")
if (requestCode == REQUEST_CODE) {
when (result?.resultCode) {
Activity.RESULT_OK -> {
val intent = result.data // received any data from another avtivity
}
Activity.RESULT_CANCELED->{
}
}
}
}
二班
val intent = Intent()
intent.putExtra(ANY_KEY, data)
setResult(Activity.RESULT_OK, intent)
finish()