android - APK 更新终止前台服务 - 报告为崩溃

标签 android service

场景是:

  • 我的应用程序正在使用活跃的前台服务运行。
  • 我选择重新安装或升级我的应用。
  • 系统将强制退出应用,包括服务。
  • 系统现在将报告我的服务崩溃。
  • 系统取代应用程序。
  • 我的应用已安装,可以启动并运行,根据需要再次启动前台服务。

有什么方法可以避免系统在杀死前台服务时报告崩溃?

正在运行的应用程序永远不会收到任何exit hook 类型的信息,然后就消失了。我无法stopForeground 这样操作系统就不会对服务的消失感到惊讶。

是否仅接受任何前台服务?我不是 Spotify 之类的,但我认为像这样的应用程序必须时不时地遇到这个问题?

如果有任何专家对此主题有深入的了解,以及可以做什么(如果有的话),我将不胜感激。当 QA 每次升级都看到崩溃时,我的(某种)企业应用程序会惹恼他们。

澄清更新:

各种 PACKAGE_* Intent 通知应用程序其他应用程序正在安装、更新或删除。他们不会告诉被替换的应用程序即将发生这种情况。

还有一个 MY_PACKAGE_REPLACED Intent 会告诉您正在更新。但它会通知新安装的版本安装是更新。

最佳答案

I have no way to stopForeground so that the OS is not surprised by the disappearance of the service.

那么你可以使用 ACTION_MY_PACKAGE_REPLACED

Broadcast Action: A new version of your application has been installed over an existing one. This is only sent to the application that was replaced. It does not contain any additional data; to receive it, just use an intent filter for this action.

[编辑]
我做了一个小研究——我所做的是,
创建了一个类 -

public class InstallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Timber.e("onReceive() %s", intent.getAction());
    }
}

在 manifest.xml 中注册了那个接收器

<receiver
            android:name=".receivers.InstallReceiver"
            android:enabled="true"
            android:exported="true">

            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_INSTALL" />
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_CHANGED" />
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
            </intent-filter>
</receiver>

...当我重新安装相同版本的应用程序(通过 ADB 运行应用程序)时,广播被调用,这是 Logcat 中的结果:

E/InstallReceiver: onReceive() android.intent.action.MY_PACKAGE_REPLACED

[编辑 2]
如果你想在应用程序被替换之前知道,我想没有办法!而且你不需要担心,因为如果应用程序被替换,进程将被系统终止。没有其他特别的事情要执行!!

了解更多信息 Follow this question

关于android - APK 更新终止前台服务 - 报告为崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47389728/

相关文章:

android - 服务在 sleep 模式下被杀死。为什么?

android - 无法应用 Arraylist

android - Fabric.io 中的 API key 和构建 secret 有什么区别?

android - 长按 EditText 时触发广播接收器

android - 通过 RESTful API 正确使用 Android 服务

android - 从服务访问传感器

android - 如何将 TextView 的值设置为 EditText 的用户输入

c# - 如何在两个 Activity 之间传递变量?

android - 如何在 map 图钉图像上设置索引号

vb.net - WCF 服务传递 TimeZoneInfo