我在Play Store上有一个应用程序。现在我想要的是,当Play Store上有新的更新时,用户在尝试使用应用程序时应该会收到一个弹出窗口来更新应用程序。如果他不更新应用程序,则应关闭应用程序。例如:我想强制用户更新应用程序以继续使用。
我在Play Store上有一个应用程序。现在我想要的是,当Play Store上有新的更新时,用户在尝试使用应用程序时应该会收到一个弹出窗口来更新应用程序。如果他不更新应用程序,则应关闭应用程序。例如:我想强制用户更新应用程序以继续使用。
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。
使用该类,您将能够实现像这样非常简单的东西:
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();
String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
或者
int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
通过共享偏好将它们存储在您的应用中。现在每当用户打开应用程序时,请检查当前versionCode与之前的版本是否匹配。如果匹配,则允许用户使用应用程序。但如果它们不匹配,则显示一个弹出窗口询问用户更新您的应用程序。