如何在 App 更新中模拟 onFailureListener & resultCode != Activity.RESULT_OK?
How to simulate onFailureListener & resultCode != Activity.RESULT_OK In App Updates?
我如何测试这个 onFailureListener & resultCode != Activity.RESULT_OK?
当我打开该应用程序时,我会根据我在 Google Play Internal Sharing 上共享的两个应用程序的版本名称创建一个强制更新状态。
首先,它会显示一个自定义对话框。在 Negative Btn -> 应用关闭
On Positive Btn -> checkForUpdate() 乐趣将开始。
我用AppUpdateType.IMMEDIATE。所以它将只显示更新的 Google 播放对话框。
我刚刚为 inAppUpdates 构建了一个版本,我正在尝试处理 onFailureListener。
不只是打印堆栈跟踪,我想打开 Google 播放,并让用户从那里安装应用程序。
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,根据文档 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 {
。我相信如果 phone 没有 Google Play 商店或者设备没有互联网连接,这个会被触发。
关于你的第二个问题,你可以check here。对于 onActivityResult,您有三个选项:
- RESULT_OK: 更新成功
- RESULT_CANCELED:用户点击后退按钮
- RESULT_FIRST_USER: 用户自定义流程
我如何测试这个 onFailureListener & resultCode != Activity.RESULT_OK?
当我打开该应用程序时,我会根据我在 Google Play Internal Sharing 上共享的两个应用程序的版本名称创建一个强制更新状态。
首先,它会显示一个自定义对话框。在 Negative Btn -> 应用关闭
On Positive Btn -> checkForUpdate() 乐趣将开始。
我用AppUpdateType.IMMEDIATE。所以它将只显示更新的 Google 播放对话框。
我刚刚为 inAppUpdates 构建了一个版本,我正在尝试处理 onFailureListener。 不只是打印堆栈跟踪,我想打开 Google 播放,并让用户从那里安装应用程序。
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,根据文档 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 {
。我相信如果 phone 没有 Google Play 商店或者设备没有互联网连接,这个会被触发。
关于你的第二个问题,你可以check here。对于 onActivityResult,您有三个选项:
- RESULT_OK: 更新成功
- RESULT_CANCELED:用户点击后退按钮
- RESULT_FIRST_USER: 用户自定义流程