- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我如何测试这个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 你有三个选项:
关于android - 如何在 App 更新中模拟 onFailureListener & resultCode != Activity.RESULT_OK?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62042312/
我是一名优秀的程序员,十分优秀!