Android AppUpdateManager 未初始化自动更新

Android AppUpdateManager not initializing the auto update

我想在每次发布更新时立即更新我的应用程序。我按照官方 android 文档中有关如何测试自动更新功能的说明进行操作,但没有任何反应。我放置了一些日志以检查这些函数是否初始化,但 logcat 也没有显示任何内容。这是我的语法问题,还是我应该将这些函数放在其他地方?目前,我所有的更新代码都写在主程序中,从应用程序的 class 开始。

关于 class

的创建方法
private static final int REQUEST_APP_UPDATE = 560;
private AppUpdateManager appUpdateManager;
private InstallStateUpdatedListener installStateUpdatedListener;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    appUpdateManager = AppUpdateManagerFactory.create(this);


    installStateUpdatedListener = new
            InstallStateUpdatedListener() {
                @Override
                public void onStateUpdate(InstallState state) {
                    if (state.installStatus() == InstallStatus.DOWNLOADED){

                    } else if (state.installStatus() == InstallStatus.INSTALLED){
                        if (appUpdateManager != null){
                            appUpdateManager.unregisterListener(installStateUpdatedListener);
                        }

                    } else {
                        Log.i(TAG, "InstallStateUpdatedListener: state: " + state.installStatus());
                    }
                }
            };

    appUpdateManager
            .getAppUpdateInfo()
            .addOnSuccessListener(
                    appUpdateInfo -> {
                        Log.d("TAG", "here");

                        // Checks that the platform will allow the specified type of update.
                        if ((appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE)
                                && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE))
                        {
                            // Request the update.
                            try {
                                Log.d("TAG", "here");
                                appUpdateManager.startUpdateFlowForResult(
                                        appUpdateInfo,
                                        AppUpdateType.IMMEDIATE,
                                        this,
                                        REQUEST_APP_UPDATE);
                            } catch (IntentSender.SendIntentException e) {
                                e.printStackTrace();
                            }
                        }
                    });

    appUpdateManager.registerListener(installStateUpdatedListener);
   ...

on resume和on stop处理方式:

 @Override
protected void onResume() {
    super.onResume();

    appUpdateManager
            .getAppUpdateInfo()
            .addOnSuccessListener(
                    appUpdateInfo -> {

                        if (appUpdateInfo.updateAvailability()
                                == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
                            // If an in-app update is already running, resume the update.
                            try {
                                appUpdateManager.startUpdateFlowForResult(
                                        appUpdateInfo,
                                        AppUpdateType.IMMEDIATE,
                                        this,
                                        REQUEST_APP_UPDATE);
                                Log.d("TAG", "tu");
                            } catch (IntentSender.SendIntentException e) {
                                e.printStackTrace();
                            }
                        }
                    });
}
@Override
protected void onStop() {
    super.onStop();
    if (appUpdateManager != null) {
        appUpdateManager.unregisterListener(installStateUpdatedListener);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_APP_UPDATE) {
        if (resultCode != RESULT_OK) {
            Log.d("TAG", "Update flow failed! Result code: " + resultCode);
            // If the update is cancelled or fails,
            // you can request to start the update again.
        }
    }
}

我建议您将所有 AppUpdateInfo 检索移动到 onResume(),因为它是 activity 更可靠的入口点(例如,如果 Activity到后台,然后被用户再次打开)。仅当 activity 被销毁时才会调用 OnCreate 方法,因为那样,如果应用程序被最小化,您可能在重新打开应用程序后看不到更新对话框。

@Override
public void onResume() {
    super.onResume();
    appUpdateManager.getAppUpdateInfo().addOnSuccessListener( info -> {
        boolean isStalledUpdate = info.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS;
        boolean isReadyForUpdate = 
            info.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
            && info.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE;

        if (isReadyForUpdate || isStalledUpdate) {
                appUpdateManager.startUpdateFlowForResult(
                    info, 
                    AppUpdateType.IMMEDIATE, 
                    this, 
                    REQ_CODE
                );
        }
}

有时应用程序不会自动与 Play 商店更新同步,因此您需要执行以下操作:

确保您有可用的手动更新 - 转到 Play 商店,检查更新并确保您有适用于您的应用程序的更新。 之后打开您的应用程序(您的 activity,在 onResume 中调用更新管理器),您将看到即时更新对话框。

另外,给你一个提示 - 让你的 Activity 像这样实现 InstallStateUpdateListener 和覆盖方法 onStateUpdate 来处理不同的事件。

@Override
public void onResume() {
    // All previous logic 
    // If update is available or was stalled call this
    appUpdateManager.registerListener(this);
}

@Override
public void onStateUpdate(InstallState state) {
    if (state == null) return;
    switch (state.installStatus()) {
        case InstallStatus.INSTALLED: 
            appUpdateManager.unregisterListener(this)
            return;
        case InstallStatus.FAILED:
            appUpdateManager.unregisterListener(this)
            return;
        case InstallStatus.CANCELED:
            appUpdateManager.unregisterListener(this)
            return;
        default:
            // provide your own logic
            return;
    }
}

这将帮助您避免在 activity 生命周期方法中调用单独的侦听器实例。

我提供的代码是有效的,不更新的问题是设备本身不知道是否有更新可用的问题。在 Play 商店刷新更新列表后,应用更新管理器在启动应用时初始化了自动更新window。