当Android Play商店中有新版本的应用程序时,强制应用程序更新

时间:2015-04-11 06:53:13

标签: android auto-update

我在Playstore上有一个应用程序。知道我想要的是当Playstore上有新的更新时,用户在尝试使用该应用程序时应该弹出一个更新应用程序。如果他没有更新应用程序,它应该关闭应用程序。例如:我想强制用户更新应用以继续使用。

4 个答案:

答案 0 :(得分:6)

据我所知,Google Play没有为此提供任何类型的API,因此您必须手动检查。

但我可以告诉你一种强制用户使用最新版本进行更新的方法。

  1. 一种方法是向用户发送推送通知,当您收到通知时,您会将用户重定向到Play商店。

  2. 第二种方法更长,但这是一种正确的方法。 您在服务器上创建Web服务,该服务器存储最新版本的应用程序。 每当您的应用运行时,

    • MainActivity上您向网络服务发帖请求并检查应用中的版本是否是最新的
    • 如果不是最新版本,请在webservice的响应中将用户重定向到playstore

答案 1 :(得分:4)

您不需要直接强制更新,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商店:

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可能会有一些小的调整。

答案 2 :(得分: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();

您可以download the class here在项目中使用。 基本上,您使用Jsoup lib通过向Google Play商店页面发出请求来获取已发布的实际版本。

答案 3 :(得分:0)

以编程方式从清单中获取versionnumber和versioncode,如:

String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;

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

通过共享首选项在应用程序中对它们进行攻击。现在,只要用户打开应用程序,就检查当前版本的前一个版本。如果它们匹配,则让用户使用app.But如果它们不匹配则显示一个弹出最多要求用户更新您的应用。

相关问题