gpt4 book ai didi

android - 如何在 App 更新中模拟 onFailureListener & resultCode != Activity.RESULT_OK?

转载 作者:行者123 更新时间:2023-12-02 09:40:13 28 4
gpt4 key购买 nike

我如何测试这个onFailureListener & resultCode != Activity.RESULT_OK

当我打开该应用程序时,我会根据我在 Google Play 内部共享上共享的两个应用程序的版本名称创建一个强制更新状态。

首先,它会显示一个自定义对话框。在 Negative Btn -> 应用关闭

On Positive Btn -> checkForUpdate() 乐趣将开始。

我使用 AppUpdateType.IMMEDIATE。所以它只会向我显示更新的 Google Play 对话框。

我刚刚为 inAppUpdates 构建了一个版本,我正在尝试处理 onFailureListener。不只是打印堆栈跟踪,我想打开 Google Play,让用户从那里安装应用程序。

fun checkForAppUpdate() {
val appUpdateManager = AppUpdateManagerFactory.create(this)
val appUpdatedListener: InstallStateUpdatedListener by lazy {
object : InstallStateUpdatedListener {
override fun onStateUpdate(installState: InstallState) {
if(installState.installStatus() == InstallStatus.INSTALLED) {
appUpdateManager.unregisterListener(this)
}
else {Log.d("inAPPtag","InstallStateUpdatedListener: state: %s" + installState.installStatus())
}
}
}
}
// Returns an intent object that you use to check for an update.
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE || appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {

appUpdateManager.registerListener(appUpdatedListener)
// Request the update.
try {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
this,
APP_UPDATE_REQUEST_CODE
)
} catch (e: IntentSender.SendIntentException) {
e.printStackTrace()
}
}
}
appUpdateInfoTask.addOnFailureListener {
it.printStackTrace()
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(Constants.STORE_URL))
if (intent.resolveActivity(this.packageManager) != null) {
splashTrace.putAttribute("SPLASH", "appUpdate")
startActivity(intent)
finish()
}
}
}

此外,如果 resultCode != Activity.RESULT_OK。同样的事情,让用户从 Google Play 安装应用程序。

if (requestCode == APP_UPDATE_REQUEST_CODE) {
if (resultCode != Activity.RESULT_OK) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(Constants.STORE_URL))
if (intent.resolveActivity(this.packageManager) != null) {
splashTrace.putAttribute("SPLASH", "appUpdate")
startActivity(intent)
finish()
}
}
}

最佳答案

您在两个地方 try catch 应用中的错误。

第一个是 SendIntentException,根据文档 here它说:

Exception thrown when trying to send through a PendingIntent that has been canceled or is otherwise no longer able to execute the request.

我想如果您从代码的另一部分触发 startUpdateFlowForResult 就会发生这种情况,然后第一个代码将被取消并触发此异常。

第二个错误发生在 appUpdateInfoTask.addOnFailureListener { 上。我相信如果手机没有 Google Play 商店或者设备没有互联网连接,这个就会被触发。

关于你的第二个问题,你可以check here .对于 onActivityResult 你有三个选项:

  1. RESULT_OK:更新成功
  2. RESULT_CANCELED:用户点击后退按钮
  3. RESULT_FIRST_USER:用户定义的流程

关于android - 如何在 App 更新中模拟 onFailureListener & resultCode != Activity.RESULT_OK?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62042312/

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