android - 如何在应用更新中模拟 onFailureListener & resultCode != Activity.RESULT_OK?
问题描述
如何测试这个onFailureListener & resultCode != Activity.RESULT_OK?
当我打开应用程序时,我会根据我在 Google Play 内部共享上共享的两个应用程序的 versionName 创建一个强制更新状态。
首先,它会显示一个自定义对话框。On Negative Btn -> 应用程序关闭
On Positive Btn -> checkForUpdate() 乐趣将开始。
我使用 AppUpdateType.IMMEDIATE。所以它只会向我显示 UPDATE 的 Google Play 对话框。
我刚刚为 inAppUpdates 构建了一个版本,我正在尝试处理 onFailureListener。不只放一个 printstacktrace,我想打开 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()
}
}
}
解决方案
您有两个地方试图在应用程序中捕获错误。
第一个是 SendIntentException,根据此处的文档,它说:
尝试通过已取消或不再能够执行请求的 PendingIntent 发送时引发异常。
我想如果您startUpdateFlowForResult
从代码的另一部分触发会发生这种情况,那么第一个将被取消并触发此异常。
第二个错误在appUpdateInfoTask.addOnFailureListener {
. 我相信如果手机没有 Google Play 商店或者设备没有互联网连接,这个会被触发。
关于你的第二个问题,你可以在这里查看。对于 onActivityResult,您有三个选项:
- RESULT_OK:更新成功
- RESULT_CANCELED:用户点击后退按钮
- RESULT_FIRST_USER:用户定义的流程
推荐阅读
- ruby-on-rails-5.1 - Rails:随机已经初始化的常量 X + 先前定义的 X
- amazon-web-services - 如何修改现有的 Amazon Cloudfront 分配?
- apache-spark - Spark无法下载kafka库
- linux - Maximum values from column is not coming while using foreach loop
- python - Python Bitwise OR
- python - Run the same computation twice on different input and then perform further computations on the two results
- swift - ARKit 条形码跟踪和视觉框架
- angular - 无法在函数离子之外获得承诺值
- node.js - Mocha 测试中的导出变量
- php - 有文件路径但也需要搜索父级