3

I am working on an application I want to give force update to app users if new version available on play store, the app should show a dialog message to user.

Arati
  • 146
  • 1
  • 1
  • 7
  • 2
    when you open your application then call web service on your server and check whether application is updated or not? And if application is updated then open your application in google play store and update your app. – Kush Patel Dec 23 '16 at 06:49
  • 1
    you can't do that, If you really want to do this, then store version name in your server, and whenever user opens app say in Splash screen, check the version stored in your server, and check the version the app installed, based on that do your things. – Mohd Asif Ahmed Dec 23 '16 at 06:50
  • 1
    You can use external api to check installed app version and latest version api. check this http://stackoverflow.com/questions/25201349/programmatically-check-play-store-for-app-updates – Praveen Dec 23 '16 at 06:50
  • do you want control on server or it should popup msg automatically? – Pradeep Deshmukh Dec 23 '16 at 06:51

6 Answers6

14
public class ForceUpdateAsync extends AsyncTask<String, String, JSONObject>{

    private String latestVersion;
    private String currentVersion;
    private Context context;
    public ForceUpdateAsync(String currentVersion, Context context){
        this.currentVersion = currentVersion;
        this.context = context;
    }

    @Override
    protected JSONObject doInBackground(String... params) {

        try {
             latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id="+context.getPackageName()+"&hl=en")
                    .timeout(30000)
                    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                    .referrer("http://www.google.com")
                    .get()
                    .select("div[itemprop=softwareVersion]")
                    .first()
                     .ownText();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }

    @Override
    protected void onPostExecute(JSONObject jsonObject) {
        if(latestVersion!=null){
            if(!currentVersion.equalsIgnoreCase(latestVersion)){
               // Toast.makeText(context,"update is available.",Toast.LENGTH_LONG).show();
                if(!(context instanceof SplashActivity)) {
                    if(!((Activity)context).isFinishing()){
                        showForceUpdateDialog();
                    }
                }
            }
        }
        super.onPostExecute(jsonObject);
    }

    public void showForceUpdateDialog(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context,
                R.style.DialogDark));

        alertDialogBuilder.setTitle(context.getString(R.string.youAreNotUpdatedTitle));
        alertDialogBuilder.setMessage(context.getString(R.string.youAreNotUpdatedMessage) + " " + latestVersion + context.getString(R.string.youAreNotUpdatedMessage1));
        alertDialogBuilder.setCancelable(false);
        alertDialogBuilder.setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())));
                dialog.cancel();
            }
        });
        alertDialogBuilder.show();
    }
}

in string.xml you can add whatever massage you want like this.

<string name="youAreNotUpdatedTitle">Update Available</string>
    <string name="youAreNotUpdatedMessage">A new version of YOUR_APP_NAME is available. Please update to version\s</string>
    <string name="youAreNotUpdatedMessage1">\s now</string>
    <string name="update">Update</string>

remember you have to define the style of your dialog in the dialog code.

now just write the forceUpdate() function in your base activity and call it inside onResume() method and you are done!!

// check version on play store and force update
    public void forceUpdate(){
        PackageManager packageManager = this.getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo =  packageManager.getPackageInfo(getPackageName(),0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String currentVersion = packageInfo.versionName;
        new ForceUpdateAsync(currentVersion,BaseActivity.this).execute();
    }
Pradeep Deshmukh
  • 754
  • 1
  • 8
  • 17
4

Store the versionCode of your app(which you have released on the playstore) on the server side. Hit the API every time user opens the app and get the versionCode. Compare the versionCode of the app user is currently using and the one you have stored on the server. Here is the code to get the versionCode of your app

PackageManager manager = this.getPackageManager();
PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
String versionCode = info.versionCode;

If the versionCode doesn't match(i.e versionCode from server > app's versionCode), prompt the user to update.

P.S If you want to use this method, you have to update versionCode on your server every time you update the app on the playstore.

Abhi
  • 2,115
  • 2
  • 18
  • 29
4

Update 2019 Android Dev Summit

Google Android Dev Summit 2019! announced Support in-app updates play app update popup with IMMEDIATE as well as FLEXIBLE App update types.

*In-app updates works only with devices running Android 5.0 (API level 21) or higher

Hardy Android
  • 855
  • 9
  • 20
1

In you first activity you can make an api call that should return the latest version of your app. Compare that with the current app version if current version is lower show a dialog asking to update. They update button can open you app in play store

pvn
  • 2,016
  • 19
  • 33
0

You can do the following things.

  1. You should have this functionality implemented in your app, which can check for the current version of your app on play store. and if a user is using the old version, then prompt them a dialog to update.
  2. your app should have any analytics (Facebook, Firebase, Localytics,etc.) SDK integrated which support In-app messages. with help of this, you can broadcast Push Notification or In-app messages to update the app.

with the help of this techniques you can ask your users to update the app.

Community
  • 1
  • 1
Gopal
  • 1,734
  • 1
  • 14
  • 34
0

Recently google announces the official way of doing this thing with the help of play core library provided by google.

There are two types of updates available - immediate update and flexible update. There are certain steps developer needs to follow for integrating the force update into his/her project.

  1. Check for update availability.
  2. Start an update
  3. Get a callback for update status
  4. Handle a flexible update
  5. Install a flexible update
  6. Handle an immediate update

There are some chances when user killed the application during update so developer needs to handle this case also which are mentioned in steps no 4 and 6.

Here is the link with the brief documentation and guide to integrate this in your project. Force update provided by google.

Happy coding ..

Shivam Yadav
  • 958
  • 11
  • 23