首页 > 解决方案 > 在后台从 BroadcastReceiver 启动 Android 服务

问题描述

如何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 上失败并出现异常,因为后台应用程序无法启动服务。日志在这里:

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

有什么办法吗?

标签: androidandroid-intent

解决方案


推荐阅读