场景是:
- 我的应用程序正在使用活跃的前台服务运行。
- 我选择重新安装或升级我的应用。
- 系统将强制退出旧应用,包括服务。
- 系统现在将报告我的服务崩溃。
- 系统取代应用程序。
- 我的新应用已安装,可以启动并运行,根据需要再次启动前台服务。
有什么方法可以避免系统在杀死前台服务时报告崩溃?
正在运行的应用程序永远不会收到任何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/