简体   繁体   English

如何检测Android App已从x版本升级到y版本?

[英]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.

相关问题 如何检测android应用何时已升级? - How to detect when android app has been upgraded? 有没有人成功地将您的 ionic cordova Hybrid 应用程序从 android 9.x 版升级到 android Cordova 10.x 版 - Has anybody successfully upgraded your ionic cordova Hybrid app from android version 9.x to android Cordova version 10.x 如何检测应用是否已被服务杀死? 安卓 - How to detect if the app has been killed using a service? android Android设备如何检测已安装应用程序? - How does an Android device detect that an app has already been installed? 检测哪个应用已在Android中启动 - Detect which app has been launched in android 检测应用程序是否已最小化android - Detect if app has been minimized android android应用升级到新版本后如何保留旧数据库? - How to retain the older database after the android app is upgraded to new version? 如何检测Android插件中的“最近”菜单中的Cordova应用已被删除? - How can I detect when a Cordova app has been removed from the recents menu, from within an Android plugin? 如何在 Android Studio 中获得已旋转的 object 的 (x, y)? - How can I get (x, y) of an object in Android Studio which has been rotated? Android chromecast:如何在应用程序进入后台之前检测应用程序启动时的投射状态? - Android chromecast: how to detect cast state on app start, before the app has been backgrounded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM