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

Error : One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts

HOW TO DISCARD UNDER REVIEW APP BUNDLE AND UPLOAD NEW APP BUNDLE

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