Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android PlayStore 'InAppUpdate' make absolutely force the Upgrade (Must Immediately Upgrade with no option to cancel what so ever)

I have below code which I have implemented the InAPPUpdate code successfully, Which is working fine as per the below screenshot. However, in this implementation User can still cancel the InAppUpdate using the (X) close button and also while downloading the app update user can still cancel the update.

My Question is there a way we can ABSOLUTELY force the end-user to update the app? Unless they update I need to stop them from using the app.

N.B: (Please don't ask why we need to force it's not the question for the discussion)

enter image description here

public class MainActivity extends BaseActivity implements TabLayout.OnTabSelectedListener, MainView {

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //check for updates
        validateAppUpdate();
}

//in app updates
    private void validateAppUpdate() {
        // Creates instance of the manager.
        appUpdateManager = AppUpdateManagerFactory.create(getBaseContext());

        // Returns an intent object that you use to check for an update.
        Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();

        // Checks that the platform will allow the specified type of update.
        appUpdateInfoTask.addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() {
            @Override
            public void onSuccess(AppUpdateInfo appUpdateInfo) {

                Log.e("AVAILABLE_VERSION_CODE", appUpdateInfo.availableVersionCode()+"");
                Log.e("AVAILABLE_VERSION_CODE_2", appUpdateInfo.updateAvailability()+"");
                //Log.e("html", "Error loading html/index.html", e);
                 if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
                        // For a flexible update, use AppUpdateType.FLEXIBLE
                        && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
                    // Request the update.

                    try {
                        appUpdateManager.startUpdateFlowForResult(
                                // Pass the intent that is returned by 'getAppUpdateInfo()'.
                                appUpdateInfo,
                                // Or 'AppUpdateType.FLEXIBLE' for flexible updates.
                                AppUpdateType.IMMEDIATE,
                                // The current activity making the update request.
                                MainActivity.this,
                                // Include a request code to later monitor this update request.
                                UPDATE_REQUEST_CODE);
                    } catch (IntentSender.SendIntentException ignored) {

                    }
                }else{
                    System.out.println("No Update Available.");
                }
            }
        });

        appUpdateManager.registerListener(installStateUpdatedListener);

    }
    //lambda operation used for below listener
    InstallStateUpdatedListener installStateUpdatedListener = installState -> {
        if (installState.installStatus() == InstallStatus.DOWNLOADED) {
            popupSnackbarForCompleteUpdate();
        }
    };


    private void popupSnackbarForCompleteUpdate() {

        Snackbar snackbar =
                Snackbar.make(
                        findViewById(android.R.id.content),
                        "Update almost finished!",
                        Snackbar.LENGTH_INDEFINITE);
        //lambda operation used for below action
        snackbar.setAction(this.getString(R.string.APP_NAME), view ->
                appUpdateManager.completeUpdate());
        snackbar.setActionTextColor(getResources().getColor(R.color.browner));
        snackbar.show();
    }
like image 708
mahen3d Avatar asked Dec 07 '25 02:12

mahen3d


1 Answers

You need to override onActivityResult() in your class which extends Activity like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != RESULT_OK) {
        validateAppUpdate();
    }
}

this will run validateAppUpdate() until the app is updated.

like image 56
Estevanbs Avatar answered Dec 08 '25 16:12

Estevanbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!