在应用程序更新中未安装最新版本的APK

时间:2019-11-01 02:28:15

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

iam成功获取显示对话框更新和下载更新,并致电appUpdateManager.completeInstall() 但是在那之后,它将重新启动应用程序,而我却没有使我的应用程序已经更新。我的应用程序保持旧版本,而在应用程序更新管理器中,一次又一次地继续显示对话框。怎么了有关信息。我在Google Play控制台中使用捆绑文件,并在应用程序更新中使用相同的密钥库FOT测试生成了一个sign.apk。请告诉我怎么了。

 private fun checkForAppUpdate() {
        // Creates instance of the manager.
        appUpdateManager = AppUpdateManagerFactory.create(this)

        // Returns an intent object that you use to check for an update.
        val appUpdateInfoTask = appUpdateManager?.appUpdateInfo

        // Create a listener to track request state updates.
        installStateUpdatedListener = InstallStateUpdatedListener { installState ->
                // Show module progress, log state, or install the update.
                if (installState.installStatus() == InstallStatus.DOWNLOADED) {
                    // After the update is downloaded, show a notification
                    // and request user confirmation to restart the app.
                    popupSnackbarForCompleteUpdateAndUnregister()
                }
            //you cannot move from this activity cause listener at this activity

            }

        // Before starting an update, register a listener for updates.
        appUpdateManager?.registerListener(installStateUpdatedListener)

        // Checks that the platform will allow the specified type of update.
        appUpdateInfoTask?.addOnSuccessListener { appUpdateInfo ->
            this.updateInfo = appUpdateInfo
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
                // Request the update.
                if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {

                    // Start an update.
                    startAppUpdateFlexible(appUpdateInfo)


                } else if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {

                    // Start an update.
                    startAppUpdateImmediate(appUpdateInfo)
                }else{
                    //no update
                    runningSplashTime()
                }
            }
        }

        appUpdateInfoTask?.addOnFailureListener {
            //runnig splash if have no update or failure
            runningSplashTime()
        }
    }
 private fun startAppUpdateImmediate(appUpdateInfo: AppUpdateInfo) {
        try {
            longToast("start update IMMEDIATE")
            appUpdateManager?.startUpdateFlowForResult(
                appUpdateInfo,
                AppUpdateType.IMMEDIATE,
                // The current activity making the update request.
                this,
                // Include a request code to later monitor this update request.
                REQ_CODE_VERSION_UPDATE_IMMEDIATE
            )
        } catch (e: IntentSender.SendIntentException) {
            e.printStackTrace()
            unregisterInstallStateUpdListener()
        }

    }
private fun startAppUpdateFlexible(appUpdateInfo: AppUpdateInfo) {
        try {
            longToast("start update Flexible")

            appUpdateManager?.startUpdateFlowForResult(
                appUpdateInfo,
                AppUpdateType.FLEXIBLE,
                // The current activity making the update request.
                this,
                // Include a request code to later monitor this update request.
                REQ_CODE_VERSION_UPDATE_FLEKSIBEL
            )
        } catch (e: IntentSender.SendIntentException) {
            e.printStackTrace()
            unregisterInstallStateUpdListener()
        }

    }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {

            REQ_CODE_VERSION_UPDATE_FLEKSIBEL -> if (resultCode !== Activity.RESULT_OK) { //RESULT_OK / RESULT_CANCELED / RESULT_IN_APP_UPDATE_FAILED
                Timber.d("Update flow failed! Result code: $resultCode")
                // If the update is cancelled or fails,
                // you can request to start the update again.
                unregisterInstallStateUpdListener()
                //user not wanna update
                runningSplashTime()
            }
            REQ_CODE_VERSION_UPDATE_IMMEDIATE -> if (resultCode !== Activity.RESULT_OK) { //RESULT_OK / RESULT_CANCELED / RESULT_IN_APP_UPDATE_FAILED
                Timber.d("Update flow failed! Result code: $resultCode")
                // If the update is cancelled or fails,
                // you can request to start the update again.
                unregisterInstallStateUpdListener()
                //user must update
                popUpUserMustbeUpdate()

            }
        }
    }
private fun popupSnackbarForCompleteUpdateAndUnregister() {
        Snackbar.make(imageView
            ,
            "An update has just been downloaded.",
            Snackbar.LENGTH_LONG
        ).apply {
            setAction("RESTART") {
                appUpdateManager?.completeUpdate()
                unregisterInstallStateUpdListener()
            }
            setActionTextColor(resources.getColor(R.color.colorPrimary))
            show()
        }

        //appUpdateManager?.completeUpdate()

    }

0 个答案:

没有答案