0

我正在实现应用内更新功能,因为我使用 google play 核心库作为implementation 'com.google.android.play:core:1.6.1.
此代码始终返回没有可用的更新。

private void checkUpdate() {
    appUpdateManager = AppUpdateManagerFactory.create(this);
    Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
    appUpdateInfoTask.addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() {
        @Override
        public void onSuccess(AppUpdateInfo appUpdateInfo) {
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                try {
                    appUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.IMMEDIATE, SplashActivity.this, 102);
                } catch (IntentSender.SendIntentException e) {
                    e.printStackTrace();
                }
                Log.e("UPDATE", "yes");
            } else {
                goToActivity();
                Log.e("UPDATE", "no");
            }
        }

    });
}
4

1 回答 1

0

确保您的班级实施InstallStateUpdatedListener

代码片段:

 private var appUpdateManager: AppUpdateManager? = null
        private var appUpdateInfo: AppUpdateInfo? = null

        private val updateAvailable = MutableLiveData<Boolean>().apply {
            value = false
        }


    appUpdateManager?.appUpdateInfo?.addOnSuccessListener {
                    if (it.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
                            it.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
                        appUpdateInfo = it
                        updateAvailable.value = true
                    } else {
                        updateAvailable.value = false
                    }
                }

        appUpdateManager?.appUpdateInfo?.addOnSuccessListener { appUpdateInfo ->
                    if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
                        updateAvailable.value = true
                    } else if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
                        if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
                            onDownloadDone()
                        }
                    }
                }

                updateAvailable.observe(this, Observer {
                    if (it) {
                        pAppUpdate.show()
                    } else {
                        pAppUpdate.gone()
                    }
                })


                pUpdateNowButton.setOnClickListener {
                    if (updateAvailable.value == true) {

                appUpdateManager?.startUpdateFlowForResult(it, AppUpdateType.FLEXIBLE, activity, REQUEST_CODE_FLEXI_UPDATE)
                    }
                }
于 2019-07-31T13:30:54.007 回答