简体   繁体   中英

Restart App after updating a new version in android

I believe that this question duplicate this Auto-restart app after market update

Anytime I publish a new version of my app in the Market, if the user had enabled the "auto update" option, the app will be updated automatically.

The app contains a service that runs constantly. But when the automatic update happens, the old running app is killed, but the new one is not started. Since the update happens mostly transparently to the user, it makes sense that the app's service should be started again automatically after the update so that there is almost no interruption of the service.

It's a bit difficult to test this with a real update from the market, so I'm using the following two adb commands to simulate this update process. Install of the 1st version:

adb install oldversion.apk // (version code is 1 ) Automatic update:

adb install -r newversion.apk // (version code is 2)

In my case, I have two activities, the first is MainActivity and the secondActivity. If the user is using the secondActivity and app is updated automatically (for me, I am using adb command to install the new version), how to trigger run MainAcitivty after the app is updated a new version successfull?

I also believe this is duplicated question. Have you tried using MY_PACKAGE_REPLACED in intent-filter for registered broadcast? (firing also when device restarts, may be useful)

manifest:

<receiver
        android:name=".OnUpdateReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

code:

public class OnUpdateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null ||
                (!Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction()) &&
                        !Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())))
            return;
        // your code, start your Service again or
        // Activity if it is REALLY desired behavior
    }
}

you can test this code by using this command, there is no need for "real" update

adb shell am broadcast -a android.intent.action.MY_PACKAGE_REPLACED -n com.yourapp/.OnUpdateReceiver

there is also support for in-app updates straight from official Google Play Core API, maybe this will solve your problem with minimal code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM