在android中更新新版本后重新启动App [英] Restart App after updating a new version in android

查看:2154
本文介绍了在android中更新新版本后重新启动App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这个问题重复了这个 在市场更新后自动重启应用

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

每当我在Market中发布我的应用程序的新版本时,如果用户启用了自动更新"选项,则该应用程序将自动更新.

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.

要通过市场上的真实更新来测试它有点困难,因此我正在使用以下两个adb命令来模拟此更新过程.安装第一个版本:

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//(版本码为1) 自动更新:

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

adb install -r newversion.apk//(版本码为2)

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

就我而言,我有两个活动,第一个是MainActivity,第二个是Activity.如果用户正在使用secondActivity并且应用程序已自动更新(对我而言,我正在使用adb命令安装新版本),那么在应用程序成功更新新版本后如何触发运行MainAcitivty?

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?

推荐答案

我也认为这是重复的问题.您是否尝试过在Intent过滤器中使用MY_PACKAGE_REPLACED进行注册广播? (设备重启时也可能会触发)

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)

清单:

<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>

代码:

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

支持应用内更新直接来自官方的Google Play Core API,也许这可以用最少的代码解决您的问题

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

这篇关于在android中更新新版本后重新启动App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆