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 he does not update the app it should close the app. Ex: I want to force the user to update the app to continue using.
问题:
回答1:
As far as I know Google Play doesn't provide with any kind of API for this, so you would have to manually check.
But I can tell you a method to force user to update with the latest release.
One way is by sending a push notification to the user, and when you receive the notification you redirect user to the playstore.
Second Method is longer but this is a proper sure method. You make a webservice on a server, which stores the latest version of the app. whenever your apps runs,
- on
MainActivity
you make a post a request to the webservice and check if the version in the app is latest or not - If it is not the latest version, on the response of the webservice you can redirect user to the playstore
- on
回答2:
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.
回答3:
I just wrote a class that helps you to know when there is a new version of your app published on Google Play Store.
With the class, you will be able to implement something really simple like this:
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();
You can download the class here and use in your project. Basically, you use Jsoup lib to get o actual version published by making a request to Google Play Store page.
回答4:
Get the versionnumber and versioncode from manifest programatically like:
String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
or
int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
Strore them in your app by shared preference.Now whenever the user open the app check the current versionCode with the previous one.If they match,then let the user to use the app.But if they dont match then show a pop up to ask the user to update your app.