我如何知道哪個應用程序版本正在被 android 替換? (How do I know which app version is being replaced with android?)


問題描述

我如何知道哪個應用程序版本正在被 android 替換? (How do I know which app version is being replaced with android?)

I have an update that I want to run for my app based on what the previous version of the app is.

For example, if the user has version 1-5 and they are upgrading to 6 or 7 I want to run the update.  Also, I don't want to run the update if they are upgrading from 6 to 7.

I've tried to accomplish this using a broadcast receiver that accepts the PACKAGE_REMOVE and PACKAGE_REPLACE intents but they don't seem to give me the information about the app that is being removed.  I don't get the PACKAGE_REMOVED intent unless I'm reinstalling the same version.  It's hard to tell because the debugger and the log print don't seem to catch before the intents are received.  Any ideas?

@Override
public void onReceive(Context context, Intent intent) {
    if (isOccurUpdated) {
        return;
    }
    if (action.equalsIgnoreCase("android.intent.action.PACKAGE_REPLACED")) {
        String name = info.versionName;
        // We are assuming that we didn't get down here if we have already
        // done this update
        Log.d(TAG,"We receive the REPLACE intent "+name);
        if (name.contains(OCCUR_ID_UPDATE) || name.contains(OCCUR_ID_UPDATE_FIX)) {
            dbh.occurIdUpdate();
        }
    } else if (action.equalsIgnoreCase("android.intent.action.PACKAGE_REMOVED")) {
        String name = info.versionName;
        if (name.contains(OCCUR_ID_UPDATE)) {
            isOccurUpdated = true;
        }
    }
}

參考解法

方法 1:

You need to store previous-version information yourself somewhere, such as:

Step #1: Create a custom Application class, and define it in your manifest (android:name attribute of <application>).

Step #2: In onCreate() of your custom Application class, read in some persistent data structure that contains the version number of your app the last time onCreate() ran. For example, you could have a lastVersion value in your SharedPreferences.

Step #3: If you see that the last version is older than your current version, do whatever upgrade logic you want.

Step #4: Write your current version out to that persistent data structure (e.g., update the SharedPreferences).

If this is more tied to database logic, SQLiteOpenHelper handles all of this for you.

(by DroidTCommonsWare)

參考文件

  1. How do I know which app version is being replaced with android? (CC BY-SA 3.0/4.0)

#upgrade #broadcastreceiver #Android #replace #version-control






相關問題

我如何知道哪個應用程序版本正在被 android 替換? (How do I know which app version is being replaced with android?)

升級到 Mac OS Lion - 如何備份 Ruby、*SQL 等? (Upgrade to Mac OS Lion - how to backup Ruby, *SQL, etc?)

將 jQuery 1.4.2 升級到 1.9.1 (Upgrade jQuery 1.4.2 to 1.9.1)

將 Jquery.min 升級到 Jquery.1.9 時出錯 (Error Upgrade Jquery.min into Jquery.1.9)

Building Wix 3.0 升級和補丁 (Building Wix 3.0 upgrade and patch)

Rails flash 拋出錯誤數量的參數(0 代表 2)錯誤 (Rails flash is throwing wrong number of arguments (0 for 2) error)

操作系統升級後無法從 ANT 訪問環境變量 (Can't access env vars from ANT after OS upgrade)

從 Rails 1.x 轉換到 2.x 是否有任何機械幫助? (Is there any mechanical assistance in converting from Rails 1.x to 2.x?)

Team Foundation Server 2015 到 2018 升級錯誤(步驟 909) (Team Foundation Server 2015 to 2018 Upgrade Error (Step 909))

運行 Tensorflow2 升級腳本會觸發編碼錯誤 (Running the Tensorflow2 upgrade Script triggers an encoding error)

將 Angular 5 升級到 Angular 11 (Upgrade angular 5 to angular 11)

在 centOS 6.9 (Final) 中將 php-5.6 升級到 7.3 時顯示多個錯誤 (Showing multiple errors while upgrading php-5.6 to 7.3 in centOS 6.9 (Final))







留言討論