Force app to update when new version of app is available in android play store

17,990

Solution 1

As far as I know Google Play doesn't provide with any kind of API for this, so you would have to manually check.

But I can tell you a method to force user to update with the latest release.

  1. One way is by sending a push notification to the user, and when you receive the notification you redirect user to the playstore.

  2. Second Method is longer but this is a proper sure method. You make a webservice on a server, which stores the latest version of the app. whenever your apps runs,

    • on MainActivity you make a post a request to the webservice and check if the version in the app is latest or not
    • If it is not the latest version, on the response of the webservice you can redirect user to the playstore

Solution 2

You shouldn't need to force an update directly, the Play store will actually automatically update your application for users when you push updates out. Users don't have to take any action unless you've made changes to your permissions.

I would definitely recommend letting the Play store do its thing on its own... but I did do similar in one app.

Something like this should tell you the play store update dates and version:

SimpleDateFormat formatter = Dates.getSimpleDateFormat(ctx, "dd MMMM yyyy");
String playUrl = "https://play.google.com/store/apps/details?id=" + appPackageName;
RestClient restClient = /* Some kind of rest client */

try {
    String playData = restClient.getAsString(playUrl);
    String versionRaw = findPattern(playData, "<([^>]?)*softwareVersion([^>]?)*>([^<]?)*<([^>]?)*>");
    String updateRaw = findPattern(playData, "<([^>]?)*datePublished([^>]?)*>([^<]?)*<([^>]?)*>");
    Date updated = formatter.parse(updateRaw.replaceAll("<[^>]*>", "").trim());
    String version = versionRaw.replaceAll("<[^>]*>", "").trim();

    _currentStatus = new PlayStatus(version, updated, new Date());
} catch (Exception e) {
    _currentStatus = new PlayStatus(PlayStatus.UNKNOWN_VERSION, new Date(0), new Date(0));
}

My PlayStatus class had a method like the following:

    public boolean hasUpdate() {
        int localVersion = 0;
        int playVersion = 0;

        if (! versionString.equals(UNKNOWN_VERSION)) {
            localVersion = Integer.parseInt(BuildConfig.VERSION_NAME.replace(".",""));
            playVersion = Integer.parseInt(versionString.replace(".",""));
        }

        return (playVersion > localVersion);
    }

You can't update the app directly obviously, but if you determine the version is out of date you can present an Intent to the user that will take them to the Play Store:

public static void updateApp(final Activity act) {
    final String appPackageName = BuildConfig.APPLICATION_ID;
    AlertDialog.Builder builder = new AlertDialog.Builder(act);
    builder
            .setTitle(act.getString(R.string.dialog_title_update_app))
            .setMessage(act.getString(R.string.dialog_google_credentials_message))
            .setNegativeButton(act.getString(R.string.dialog_default_cancel), null)
            .setPositiveButton(act.getString(R.string.dialog_got_it), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    try {
                        act.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
                    } catch (ActivityNotFoundException anfe) {
                        act.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName;)));
                    }
                }
            });
    AlertDialog dialog = builder.create();
    dialog.show();
}

I believe this was compiled against API 21, so there might be a couple small tweaks for 22.

Solution 3

I just wrote a class that helps you to know when there is a new version of your app published on Google Play Store.

With the class, you will be able to implement something really simple like this:

new CheckNewAppVersion(yourContext).setOnTaskCompleteListener(new CheckNewAppVersion.ITaskComplete() {
    @Override
    public void onTaskComplete(CheckNewAppVersion.Result result) {

        //Checks if there is a new version available on Google PlayStore.
        result.hasNewVersion();

        //Get the new published version code of the app.
        result.getNewVersionCode();

        //Get the app current version code.
        result.getOldVersionCode();

        //Opens the Google Play Store on your app page to do the update.
        result.openUpdateLink();
    }
}).execute();

You can download the class here and use in your project. Basically, you use Jsoup lib to get o actual version published by making a request to Google Play Store page.

Share:
17,990

Related videos on Youtube

Neerav Shah
Author by

Neerav Shah

Updated on September 15, 2022

Comments

  • Neerav Shah
    Neerav Shah over 1 year

    I had an app on the playstore. Know what I want is when new update is available on playstore the user should get a popup to update the app when he try to use the app. And if he does not update the app it should close the app. Ex: I want to force the user to update the app to continue using.

  • RobertB
    RobertB almost 8 years
    I specifically block the Play Store from performing automatic app updates - I want to know what's changing before I allow it. So depending on the auto update feature defeats the purpose.
  • Crias
    Crias almost 8 years
    That's why I included the code I've used in the past to do a "forced-update". Since then the work for that app has been taken over by some guys that know mobile better than me and I believe they're using push updates now as well to move app versions forward. It does break the usual Play Store experience though. I don't open many of my apps for long periods of time, but I sometimes pop them open when I see an auto-update come through. I get wanting to feel "in control" of your own device, but is blocking auto-update what your users actually want?