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

前端 未结 4 1810
暗喜
暗喜 2020-12-10 15:36

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

4条回答
  •  无人及你
    2020-12-10 15:57

    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.

提交回复
热议问题