我在一个应用程序上工作,我想给应用程序用户强制更新,如果在play商店上有新版本,应用程序应该会向用户显示对话框消息。
发布于 2016-12-23 15:05:27
public class ForceUpdateAsync extends AsyncTask<String, String, JSONObject>{
private String latestVersion;
private String currentVersion;
private Context context;
public ForceUpdateAsync(String currentVersion, Context context){
this.currentVersion = currentVersion;
this.context = context;
}
@Override
protected JSONObject doInBackground(String... params) {
try {
latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id="+context.getPackageName()+"&hl=en")
.timeout(30000)
.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
.referrer("http://www.google.com")
.get()
.select("div[itemprop=softwareVersion]")
.first()
.ownText();
} catch (IOException e) {
e.printStackTrace();
}
return new JSONObject();
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
if(latestVersion!=null){
if(!currentVersion.equalsIgnoreCase(latestVersion)){
// Toast.makeText(context,"update is available.",Toast.LENGTH_LONG).show();
if(!(context instanceof SplashActivity)) {
if(!((Activity)context).isFinishing()){
showForceUpdateDialog();
}
}
}
}
super.onPostExecute(jsonObject);
}
public void showForceUpdateDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context,
R.style.DialogDark));
alertDialogBuilder.setTitle(context.getString(R.string.youAreNotUpdatedTitle));
alertDialogBuilder.setMessage(context.getString(R.string.youAreNotUpdatedMessage) + " " + latestVersion + context.getString(R.string.youAreNotUpdatedMessage1));
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton(R.string.update, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())));
dialog.cancel();
}
});
alertDialogBuilder.show();
}
}在string.xml中,你可以像这样添加任何你想要的消息。
<string name="youAreNotUpdatedTitle">Update Available</string>
<string name="youAreNotUpdatedMessage">A new version of YOUR_APP_NAME is available. Please update to version\s</string>
<string name="youAreNotUpdatedMessage1">\s now</string>
<string name="update">Update</string>请记住,您必须在对话框代码中定义对话框的样式。
现在,只需在基本活动中编写forceUpdate()函数,并在onResume()方法中调用它,就完成了!!
// check version on play store and force update
public void forceUpdate(){
PackageManager packageManager = this.getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo = packageManager.getPackageInfo(getPackageName(),0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
String currentVersion = packageInfo.versionName;
new ForceUpdateAsync(currentVersion,BaseActivity.this).execute();
}发布于 2016-12-23 14:57:04
在服务器端存储您的应用程序(已在playstore上发布)的versionCode。每次用户打开应用程序时点击应用程序接口并获取versionCode。将用户当前使用的应用程序的versionCode与您存储在服务器上的you进行比较。以下是获取应用程序versionCode的代码
PackageManager manager = this.getPackageManager();
PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
String versionCode = info.versionCode;如果versionCode不匹配(即来自服务器>应用程序的versionCode的versionCode),则提示用户更新。
附注:如果你想使用这种方法,你必须在每次更新playstore上的应用程序时更新服务器上的versionCode。
发布于 2019-07-17 16:56:08
更新2019安卓开发者峰会
谷歌Android开发者峰会2019!宣布Support in-app updates play应用程序更新弹出窗口,带有IMMEDIATE以及FLEXIBLE应用程序更新类型。
*应用内更新仅适用于运行Android 5.0 (API level 21)或更高版本的设备
https://stackoverflow.com/questions/41296491
复制相似问题