在后台运行应用程序时,如何从BroadcastReceiver
启动Android服务?
使用案例如下:其他应用可以通过向我的应用发送轮询广播(作为定向广播,由清单声明的接收者接收)来向我的应用请求数据。然后,广播接收器将启动提供数据的服务(如果尚未运行)。然后该服务将检索数据(除非它已经在运行并且已经缓存了数据)并回答请求(通过发送另一个广播)。
这是我尝试过的代码:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/* Start the service with the internal poll intent */
Intent outIntent = new Intent(Const.ACTION_INTERNAL_POLL, null, context, MyService.class);
context.startService(outIntent);
}
}
这在较旧的Android版本上有效,但在Android 8上却失败,因为后台的应用无法启动服务。 Logcat在这里:
07-19 21:44:56.334 11096 11096 E AndroidRuntime: java.lang.RuntimeException: Unable to start receiver org.traffxml.roadeagle.android.core.MyReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { act=my.app.POLL cmp=my.app/.android.core.MyReceiver (has extras) }: app is in background uid UidRecord{dfb1a59 u0a169 RCVR idle change:idle|uncached procs:1 seq(0,0,0)}
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:3194)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at android.app.ActivityThread.-wrap17(Unknown Source:0)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at android.os.Looper.loop(Looper.java:164)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6494)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=my.app.POLL cmp=my.app/.android.core.TraffReceiver (has extras) }: app is in background uid UidRecord{dfb1a59 u0a169 RCVR idle change:idle|uncached procs:1 seq(0,0,0)}
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1521)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1477)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:650)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:650)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at my.app.android.core.MyReceiver.onReceive(MyReceiver.java:40)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:3187)
07-19 21:44:56.334 11096 11096 E AndroidRuntime: ... 8 more
有什么办法解决吗?