Code to Update App Automatically From Google PlayStore

 

To prompt users to download App automatically from Google PlayStore as soon as an App update is available, use following code:


In build.gradle (App Level) file, Add,

implementation 'com.google.android.play:app-update:2.1.0'


Sync Project


Next add in you MainActivity Class( or any Activity that comes after SplashActivity, where I prefer to put this code)


Add the following code

private static final int REQUEST_CODE_UPDATE = 123;

private AppUpdateManager appUpdateManager;


Now inside onCreate() Method, add the following code


appUpdateManager = AppUpdateManagerFactory.create(this);

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

    if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE

            && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {

        try {

            appUpdateManager.startUpdateFlowForResult(

                    appUpdateInfo,

                    AppUpdateType.FLEXIBLE,

                    this,

                    REQUEST_CODE_UPDATE

            );

        } catch (Exception e) {

            e.printStackTrace();

        }

    }


});


Comments

Popular posts from this blog

Problems To Upgrade Android Gradle Plugin from 4.1.3 to 8.1.2 and solutions

How to Add Animation to Android App in JAVA

Get Android App Version No and Display in a Text Field