调用IntentSenderRequest.Builder传入Boolean参数触发编译错误
解决IntentSenderRequest.Builder构造函数编译错误
错误原因
你误用了Play Core的API:appUpdateManager.startUpdateFlowForResult()返回的是Boolean类型(仅用于表示更新流是否成功启动),但你把它赋值给intentSender变量并传给IntentSenderRequest.Builder——而这个构造函数只接受IntentSender或PendingIntent类型,类型完全不匹配,因此触发编译错误。
修复方案
提供两种适配Activity Result API的正确实现方式,按需选择:
方式1:使用Play Core专属重载方法(推荐)
直接调用startUpdateFlow()的重载版本,它会自动处理IntentSenderRequest的创建与启动,代码更简洁:
private fun startUpdate(appUpdateInfo: com.google.android.play.core.appupdate.AppUpdateInfo, updateType: Int) { try { val updateOptions = AppUpdateOptions.newBuilder(updateType).build() // 直接传入ActivityResultLauncher,无需手动构建请求 appUpdateManager.startUpdateFlow( appUpdateInfo, updateResultLauncher, updateOptions ) updateState.value = UpdateState.Downloading } catch (e: Exception) { Log.e("InAppUpdateManager", "Ошибка при запуске обновления", e) updateState.value = UpdateState.Error(e.message ?: "Неизвестная ошибка") } }
方式2:手动从AppUpdateInfo获取IntentSender
如果需要自定义IntentSenderRequest的参数,可以从appUpdateInfo中直接获取合法的IntentSender实例:
private fun startUpdate(appUpdateInfo: com.google.android.play.core.appupdate.AppUpdateInfo, updateType: Int) { try { val updateOptions = AppUpdateOptions.newBuilder(updateType).build() // 从AppUpdateInfo中获取正确的IntentSender对象 val intentSender = appUpdateInfo.intentSender val intentSenderRequest = IntentSenderRequest.Builder(intentSender).build() updateResultLauncher.launch(intentSenderRequest) updateState.value = UpdateState.Downloading } catch (e: Exception) { Log.e("InAppUpdateManager", "Ошибка при запуске обновления", e) updateState.value = UpdateState.Error(e.message ?: "Неизвестная ошибка") } }
补充说明
startUpdateFlowForResult()是为传统onActivityResult回调设计的旧API,它的返回值仅作为启动结果的反馈,不会返回IntentSender。当使用Jetpack Activity Result API时,必须使用适配的新方法或从AppUpdateInfo中获取IntentSender,才能正确构建请求。
内容的提问来源于stack exchange,提问作者IbremMiner837
相关产品推荐
相关产品推荐

