How to force update in Android application if new version is available?

血红的双手。 提交于 2020-01-29 09:57:24

问题


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.


回答1:


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();
    }



回答2:


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




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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 ..



来源:https://stackoverflow.com/questions/41296491/how-to-force-update-in-android-application-if-new-version-is-available

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!