Android Oreo 应用更新后如何重启前台服务?

How to restart foreground service after app updating in Android Oreo?

我们需要在应用程序从 Google Play 自动更新后重新启动我们应用程序的前台服务。对于 SDK < 26,我们在 PACKAGE_REPLACED actoin 上注册了接收器,一切正常。如果禁止隐式意图,我们如何在 SDK 26 中做同样的事情?谢谢

首先,并非所有隐式广播都被禁止。有a whitelist.

其次,ACTION_MY_PACKAGE_CHANGED is an explicit broadcast, sent only to your app. Quoting the documentation

Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. An implicit broadcast is a broadcast that does not target that app specifically. For example, ACTION_PACKAGE_REPLACED is an implicit broadcast, since it is sent to all registered listeners, letting them know that some package on the device was replaced. However, ACTION_MY_PACKAGE_REPLACED is not an implicit broadcast, since it is sent only to the app whose package was replaced, no matter how many other apps have registered listeners for that broadcast.

因此,您应该可以注册 ACTION_MY_PACKAGE_REPLACED,并在那里重新启动您的服务。