新版本可用时强制更新Android应用程序
我在谷歌Play商店有一个应用程序。当更新版本可用时,旧版本将变得不可用–也就是说,如果用户不更新应用程序,他们就不会进入应用程序。当新版本可用时,我如何强制用户更新应用程序?新版本发布后,您不应该停止支持旧版本。这将导致糟糕的用户体验。我不知道有哪家软件供应商会这样做,这是有充分理由的 如果用户当时无法更新或不想更新,会发生什么情况?他们根本无法使用你的应用程序,这很糟糕新版本可用时强制更新Android应用程序,android,Android,我在谷歌Play商店有一个应用程序。当更新版本可用时,旧版本将变得不可用–也就是说,如果用户不更新应用程序,他们就不会进入应用程序。当新版本可用时,我如何强制用户更新应用程序?新版本发布后,您不应该停止支持旧版本。这将导致糟糕的用户体验。我不知道有哪家软件供应商会这样做,这是有充分理由的 如果用户当时无法更新或不想更新,会发生什么情况?他们根本无法使用你的应用程序,这很糟糕 谷歌没有提供任何版本跟踪选项,所以你必须自己进行。一个简单的web服务返回应用程序可以检查的当前实时版本就足够了。然后,您
谷歌没有提供任何版本跟踪选项,所以你必须自己进行。一个简单的web服务返回应用程序可以检查的当前实时版本就足够了。然后,您可以更新版本,应用程序将知道它已过时。我只建议你使用它来让你的用户知道有一个更新比依靠谷歌播放更快。它实际上不应该用来阻止应用程序工作,而只是用来提示用户进行更新 我同意斯科特·赫尔姆在另一个答案中的观点。但在某些极端情况下(安全问题、API破坏性更改…),您绝对需要用户更新以继续使用应用程序,您可以提供一个简单的版本控制API。API如下所示: versionCheck API: 请求参数:
int-appVersion
boolean forceUpgrade
boolean建议升级
forceUpgrade
为true
,则显示一个弹出对话框,其中包含允许用户退出应用程序或转到Google Play Store升级应用程序的选项
否则,如果recommendUpgrade
为true
,则显示带有更新或继续使用应用程序选项的弹出对话框
即使有了这种强制升级功能,您也应该继续支持旧版本,除非绝对需要。Scott和Michael的答案是正确的。托管提供您支持的最小版本号的服务,并将其与安装的版本进行比较。你应该希望永远不需要使用这个,但它是一个救命稻草,如果有一些版本,你绝对必须杀死由于一些严重的缺陷 我只是想为下一步做什么添加代码。下面是如何启动Google Play Intent,并在提示用户必须升级后将其带到商店中的新版本
public class UpgradeActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upgrade);
final String appName = "com.appname";
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+appName)));
}
});
}
}
如果必须在每个版本上强制升级,则应重新考虑您的设计。最好定义我们自己的升级过程
--- we can do upgrade compulsory or optionally.
String currentVersion, latestVersion;
Dialog dialog;
private void getCurrentVersion(){
PackageManager pm = this.getPackageManager();
PackageInfo pInfo = null;
try {
pInfo = pm.getPackageInfo(this.getPackageName(),0);
} catch (PackageManager.NameNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
currentVersion = pInfo.versionName;
new GetLatestVersion().execute();
}
private class GetLatestVersion extends AsyncTask<String, String, JSONObject> {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(String... params) {
try {
//It retrieves the latest version by scraping the content of current version from play store at runtime
Document doc = Jsoup.connect(urlOfAppFromPlayStore).get();
latestVersion = doc.getElementsByClass("htlgb").get(6).text();
}catch (Exception e){
e.printStackTrace();
}
return new JSONObject();
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
if(latestVersion!=null) {
if (!currentVersion.equalsIgnoreCase(latestVersion)){
if(!isFinishing()){ //This would help to prevent Error : BinderProxy@45d459c0 is not valid; is your activity running? error
showUpdateDialog();
}
}
}
else
background.start();
super.onPostExecute(jsonObject);
}
}
private void showUpdateDialog(){
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("A New Update is Available");
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse
("market://details?id=yourAppPackageName")));
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
background.start();
}
});
builder.setCancelable(false);
dialog = builder.show();
}
在升级过程之前,如果数据库架构发生任何更改,请确保您处理了正确的数据库迁移过程。尝试以下操作:
首先,您需要对playstore链接进行请求调用,从那里获取当前版本,然后将其与当前版本进行比较
--- we can do upgrade compulsory or optionally.
String currentVersion, latestVersion;
Dialog dialog;
private void getCurrentVersion(){
PackageManager pm = this.getPackageManager();
PackageInfo pInfo = null;
try {
pInfo = pm.getPackageInfo(this.getPackageName(),0);
} catch (PackageManager.NameNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
currentVersion = pInfo.versionName;
new GetLatestVersion().execute();
}
private class GetLatestVersion extends AsyncTask<String, String, JSONObject> {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(String... params) {
try {
//It retrieves the latest version by scraping the content of current version from play store at runtime
Document doc = Jsoup.connect(urlOfAppFromPlayStore).get();
latestVersion = doc.getElementsByClass("htlgb").get(6).text();
}catch (Exception e){
e.printStackTrace();
}
return new JSONObject();
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
if(latestVersion!=null) {
if (!currentVersion.equalsIgnoreCase(latestVersion)){
if(!isFinishing()){ //This would help to prevent Error : BinderProxy@45d459c0 is not valid; is your activity running? error
showUpdateDialog();
}
}
}
else
background.start();
super.onPostExecute(jsonObject);
}
}
private void showUpdateDialog(){
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("A New Update is Available");
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse
("market://details?id=yourAppPackageName")));
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
background.start();
}
});
builder.setCancelable(false);
dialog = builder.show();
}
字符串当前版本,最新版本;
对话;
私有void getCurrentVersion(){
PackageManager pm=this.getPackageManager();
PackageInfo pInfo=null;
试一试{
pInfo=pm.getPackageInfo(this.getPackageName(),0);
}捕获(PackageManager.NameNotFoundException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
currentVersion=pInfo.versionName;
新建GetLatestVersion().execute();
}
私有类GetLatestVersion扩展了AsyncTask{
私有进程对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的JSONObject doInBackground(字符串…参数){
试一试{
//它通过在运行时从play store中删除当前版本的内容来检索最新版本
Document doc=Jsoup.connect(urlOfAppFromPlayStore.get();
latestVersion=doc.getElementsByClass(“htlgb”).get(6.text();
}捕获(例外e){
e、 printStackTrace();
}
返回新的JSONObject();
}
@凌驾
受保护的void onPostExecute(JSONObject JSONObject){
如果(最新版本!=null){
如果(!currentVersion.equalsIgnoreCase(最新版本)){
如果(!isFinishing()){//,这将有助于防止错误:BinderProxy@45d459c0无效;您的活动正在运行吗?错误
showUpdateDialog();
}
}
}
其他的
background.start();
super.onPostExecute(jsonObject);
}
}
私有void showUpdateDialog(){
final AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setTitle(“有新的更新可用”);
setPositiveButton(“更新”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
startActivity(新意图)(Intent.ACTIO
class versionChecker extends AsyncTask<String, String, String> {
String newVersion;
@Override
protected String doInBackground(String... params) {
try {
newVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=+YOR_PACKAGE_NAME+&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 newVersion;
}
}
public class UpdateMeeDialog {
ActivityManager am;
TextView rootName;
Context context;
Dialog dialog;
String key1,schoolId;
public void showDialogAddRoute(Activity activity, final String packageName){
context=activity;
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog_update);
am = (ActivityManager)activity.getSystemService(Context.ACTIVITY_SERVICE);
Button cancelDialogue=(Button)dialog.findViewById(R.id.buttonUpdate);
Log.i("package name",packageName);
cancelDialogue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/details?
id="+packageName+"&hl=en"));
context.startActivity(intent);
}
});
dialog.show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d4e9f2">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Please Update First..!!"
android:textSize="20dp"
android:textColor="#46a5df"
android:textAlignment="center"
android:layout_marginTop="50dp"
android:id="@+id/textMessage"
/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="1"
android:layout_marginTop="50dp"
android:layout_below="@+id/textMessage"
android:layout_height="50dp">
<Button
android:id="@+id/buttonUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Update"
android:background="#67C6F1"
android:textAlignment="center" />
</LinearLayout>
private int getCurrentVersionCode() {
try {
return getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return -1;
dependencies {
implementation 'com.google.android.play:core:1.5.0'
...
}
var activeVersion = "3.9.2";
var currentVersion = "3.9.1";
var activeVersionArr = activeVersion.split(".");
var currentVersionArr = currentVersion.split(".");
var compareArray =JSON.stringify(activeVersionArr)==JSON.stringify(currentVersionArr);
if(compareArray){
return false;
}
if(!compareArray){
for(var i=0;i<activeVersionArr.length;i++){
if(activeVersionArr[i]!==currentVersionArr[i]){
if(activeVersionArr[i] > currentVersionArr[i]){
return true;
}else{
return false;
}
}
}
}