在安卓应用商店有新版本时强制应用更新

11

我在Play Store上有一个应用程序。现在我想要的是,当Play Store上有新的更新时,用户在尝试使用应用程序时应该会收到一个弹出窗口来更新应用程序。如果他不更新应用程序,则应关闭应用程序。例如:我想强制用户更新应用程序以继续使用。


这个开源的GitHub项目(MAHAndroidUpdater)完全提供了此功能。试试吧,非常简单。https://github.com/hummatli/MAHAndroidUpdater - Sattar Hummatli
4个回答

6
据我所知,Google Play没有提供这方面的任何API,因此您需要手动检查。
但是我可以告诉您一种强制用户更新最新版本的方法。
以下是两种方法:
1. 通过向用户发送推送通知的方式,当您收到通知后将用户重定向到Play商店。
2. 第二种方法比较长,但是这是一种确保成功的方法。您可以在服务器上创建一个Web服务,存储应用程序的最新版本。每当您的应用程序运行时,
- 在 MainActivity 中,您可以向 Web 服务发出POST请求并检查应用程序中的版本是否最新。 - 如果不是最新版本,则在 Web 服务的响应中,您可以将用户重定向到 Play 商店。

5
您不需要直接强制更新,当您推出更新时,Play商店会自动为用户更新您的应用程序。除非您更改了权限,否则用户无需采取任何行动。
我强烈建议让Play商店自己完成更新...但我在一个应用程序中确实做到了类似的事情。
像这样的内容应该告诉您Play商店的更新日期和版本:
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));
}

我的PlayStatus类有一个类似以下的方法:

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

显然你无法直接更新应用程序,但是如果您确定版本已经过时,您可以向用户提供一个意图,将他们带到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();
}

我相信这是针对API 21编译的,所以可能需要进行一些小的调整才能适配22。


我特意阻止Play商店执行自动应用程序更新 - 我想在允许更新之前知道发生了什么变化。因此,依赖于自动更新功能是没有意义的。 - RobertB
1
这就是为什么我包含了我过去使用的代码来执行“强制更新”的原因。自那时以来,该应用程序的工作已被一些比我更懂移动设备的人接管,并且我相信他们现在也在使用推送更新来推进应用程序版本。但它确实破坏了通常的Play Store体验。我很少长时间打开我的应用程序,但有时当我看到自动更新时,我会打开它们。我理解你想要感觉“掌控”自己的设备,但是阻止自动更新真的是你的用户想要的吗? - Crias

1
我刚刚编写了一个类,可以帮助你知道在Google Play商店上发布了新版本的应用程序。

使用该类,您将能够实现像这样非常简单的东西:

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

你可以在这里下载课程并在你的项目中使用。 基本上,你需要使用Jsoup库来获取实际版本,方法是向Google Play商店页面发送请求。

0
以编程方式从清单文件中获取版本号和版本代码,例如:
String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;

或者

int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;

通过共享偏好将它们存储在您的应用中。现在每当用户打开应用程序时,请检查当前versionCode与之前的版本是否匹配。如果匹配,则允许用户使用应用程序。但如果它们不匹配,则显示一个弹出窗口询问用户更新您的应用程序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接