【问题标题】:How to simulate onFailureListener & resultCode != Activity.RESULT_OK In App Updates?如何在应用更新中模拟 onFailureListener & resultCode != Activity.RESULT_OK?
【发布时间】:2020-05-27 11:44:04
【问题描述】:

我如何测试这个 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()
                }
            }
        }

【问题讨论】:

    标签: android kotlin google-play in-app-update


    【解决方案1】:

    您有两个地方试图在您的应用中发现错误。

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

    尝试通过已取消或无法再执行请求的 PendingIntent 发送时抛出异常。

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

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

    关于第二个问题,您可以check here。对于 onActivityResult,您有三个选项:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 2013-02-02
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多