Android AppUpdateManager not initializing the auto update

Viewed 1690

I want to update my app immediately every time I publish an update. I followed the instructions from the official android documentation on how to test the auto update feature , but nothing happens. I placed some logs in order to check whether these functions initialize, but the logcat shows nothing either. Is this a problem in my syntax, or should I place these functions somewhere else? Currently, all my update code is written inside the main, starting class of the app.

On create method of the 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 and on stop handling methods:

 @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.
        }
    }
}
2 Answers

I suggest you to move all your AppUpdateInfo retrieving to onResume() since it is more reliable entry point of an activity (for example, if Activity has came to background and then was opened again by user). OnCreate method will be called only if activity was destroyed, since that, you may not see update dialog after reopening application if it was minimized.

@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
                );
        }
}

Sometimes app is not automatically synchronized with Play Store updates, so you need to do the following:

Make sure you have an update available manually - go to Play Store, check for updates and make sure that you have an update available for your application. After that open your app (your activity, that calls update manager in onResume) and you will see immediate update dialog.

Also, one tip for you - make your Activity implement InstallStateUpdateListener and override method onStateUpdate like this to handle different events.

@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;
    }
}

This will help you to avoid calling separate instance of listener in activity lifecycle methods.

The code that I provided was working, the problem of not updating was a matter of the device itself not knowing whether an update is available. After refreshing the update list on the Play Store, the app update manager initialized the auto update window when starting the app.

Related