[英]How to detect Android App has been upgraded from version x to y?
I have a problem with my android app for own educational reason. 由于自身的教育原因,我的Android应用存在问题。
Is there a ready solution to detect that the app has now been updated from version x to y? 是否有现成的解决方案来检测该应用程序现在已从版本x更新为y? Because I want to migrate some data once only if the app was installed in a special version before.
因为我只想在以前以特殊版本安装应用程序时才迁移一些数据。
Thanks. 谢谢。
The easiest way is to store the last known version of your application in SharedPreferences, and check that when the app is launched. 最简单的方法是将应用程序的最新已知版本存储在SharedPreferences中,并检查该应用程序何时启动。
You can get the current version code of your application with BuildConfig.VERSION_CODE
. 您可以使用
BuildConfig.VERSION_CODE
获取应用程序的当前版本代码。
For example: 例如:
@Override
public void onStart() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int lastKnownVersion = prefs.getInt("lastKnownAppVersion", 0);
if (lastKnownVersion < BuildConfig.VERSION_CODE) {
// Your app has been updated
prefs.edit().putInt("lastKnownAppVersion", BuildConfig.VERSION_CODE);
}
}
Registering a BroadcastReceiver to listen for the ACTION_MY_PACKAGE_REPLACED
intent will tell you when your package is replaced (ie updated), but that won't tell you what version was previously installed. 注册一个BroadcastReceiver来监听
ACTION_MY_PACKAGE_REPLACED
意图将告诉您何时更换(即更新了)您的软件包,但不会告诉您以前安装了哪个版本。
Here is a simple way that can solve your problem. 这是解决问题的一种简单方法。 In your code you can easily get the app versionCode and versionName.
在您的代码中,您可以轻松获取应用程序versionCode和versionName。 In your home activity you check isAppUpdated().
在家庭活动中,检查isAppUpdated()。 if current APP VERSION_NAME is not matched with stored VERSION_NAME then app is updated.
如果当前的APP VERSION_NAME与存储的VERSION_NAME不匹配,则会更新该应用。
Here is sample code: 这是示例代码:
Save VersionName in preferences: 将VersionName保存在首选项中:
public static void saveVersionNameAndCode(Context context){
try{
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
int versionCode = packageInfo.versionCode;
String versionName=packageInfo.versionName;
CommonTasks.showLog("Saving APP VersionCode and Version Name");
// SAVE YOUR DATA
}catch(Exception e){
}
}
Check App is Upgraded: 检查应用程序已升级:
public static boolean isAppUpdated(Context context){
boolean result=false;
try{
PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
int versionCode = packageInfo.versionCode;
String versionName=packageInfo.versionName;
String prevVersionName= GET STORED VERSION_NAME;
if(prevVersionName!=null && !prevVersionName.equals("") &&
!prevVersionName.equals(versionName)){
showLog("App Updated");
result=true;
}else{
showLog("App Not updated");
}
}catch(Exception e){
}
return result;
}
here, if isAppUpdated() return true means your app is updated. 在这里,如果isAppUpdated()返回true,则表示您的应用已更新。
Note, After checking upgrade issue, you must have to update the sharedPreferences. 注意, 检查升级问题后,必须必须更新sharedPreferences。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.