0

我已经为FLEXIBLE in-app-update android 尝试了下面的代码,当单击对话框中的更新时,它开始在后台从 play-store 下载新更新,我已将侦听器添加为InstallStateUpdatedListener,但它不会通知我何时下载已完成,还检查了onActivityResult()但没有奏效。

后台下载完成后如何收到通知?

  AppUpdateManager appUpdateManager;     
  int UPDATE_REQUEST_CODE = 1;

private void updateTheApp() {
   appUpdateManager = AppUpdateManagerFactory.create(this);

    Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();

    appUpdateInfoTask.addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() {
        @Override
        public void onSuccess(AppUpdateInfo appUpdateInfo) {

            Log.e("AVAILABLE_VERSION_CODE", appUpdateInfo.availableVersionCode());
            if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                    && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {

                try {
                    appUpdateManager.startUpdateFlowForResult(
                            appUpdateInfo,
                            AppUpdateType.IMMEDIATE,
                            MainActivity.this,
                            UPDATE_REQUEST_CODE);
                } catch (IntentSender.SendIntentException ignored) {

                }
            }
        }
    });

    appUpdateManager.registerListener(installStateUpdatedListener);
    appUpdateManager.unregisterListener(installStateUpdatedListener);
}
4

2 回答 2

0

创建一个可以监听后台更新的监听器,在state.installStatus()状态为下载后,InstallStatus.DOWNLOADED我们也必须将更新标记为完成。

InstallStateUpdatedListener listener = new InstallStateUpdatedListener() {
        @Override
        public void onStateUpdate(InstallState state) {
            Log.d("installState", state.toString());
            if (state.installStatus() == InstallStatus.DOWNLOADED) {
                // After the update is downloaded, show a notification
                // and request user confirmation to restart the app.
                popupSnackbarForCompleteUpdate();
            }
        }
    };

在创建实例之后,您可以在 onCreate() 中请求 InAppUpdate

appUpdateManger = AppUpdateManagerFactory.create(context);

你可以注册监听器。

popupSnackbarForCompleteUpdate() 方法,

private void popupSnackbarForCompleteUpdate() {
        Snackbar snackbar =
                Snackbar.make(findViewById(android.R.id.content), "An update has just been downloaded.", Snackbar.LENGTH_INDEFINITE);
        snackbar.setAction("Restart", view -> appUpdateManager.completeUpdate());
        snackbar.setActionTextColor(getResources().getColor(android.R.color.white));
        snackbar.show();
    }

完成这项工作后,您还必须取消注册侦听器。

 @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("installState", "destroy");
        appUpdateManager.unregisterListener(listener);
    }
于 2019-08-21T14:02:18.567 回答
0
appUpdateManager.registerListener(installStateUpdatedListener);
appUpdateManager.unregisterListener(installStateUpdatedListener);

您在注册后立即删除监听器

于 2019-08-12T18:49:32.213 回答