Android 8.0 启动后台service 出错 IllegalStateException: Not allowed to start service Intent

出错信息

E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: com.cjl.servicetest, PID: 4455
E AndroidRuntime: java.lang.RuntimeException: Unable to create application com.cjl.servicetest.App: java.lang.IllegalStateException: Not allowed to start service Intent { act=lock cmp=com.cjl.servicetest/com.cjl.servicetest.service.KeyguardService (has extras) }: app is in background uid UidRecord{9d2827c u0a19 RCVR idle change:uncached procs:1 seq(0,0,0)}
E AndroidRuntime:   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5770)
E AndroidRuntime:   at android.app.ActivityThread.-wrap1(Unknown Source:0)
E AndroidRuntime:   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1680)
E AndroidRuntime:   at android.os.Handler.dispatchMessage(Handler.java:106)
E AndroidRuntime:   at android.os.Looper.loop(Looper.java:164)
E AndroidRuntime:   at android.app.ActivityThread.main(ActivityThread.java:6523)
E AndroidRuntime:   at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime:   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
E AndroidRuntime:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
E AndroidRuntime: Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=lock cmp=com.cjl.servicetest/com.cjl.servicetest.service.KeyguardService (has extras) }: app is in background uid UidRecord{9d2827c u0a19 RCVR idle change:uncached procs:1 seq(0,0,0)}
E AndroidRuntime:   at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1522)
E AndroidRuntime:   at android.app.ContextImpl.startService(ContextImpl.java:1478)
E AndroidRuntime:   at android.content.ContextWrapper.startService(ContextWrapper.java:650)
E AndroidRuntime:   at com.cjl.servicetest.service.KeyguardService.a(KeyguardService.java:80)
E AndroidRuntime:   at com.cjl.servicetest.DAApp.onCreate(DAApp.java:388)
E AndroidRuntime:   at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1119)
E AndroidRuntime:   at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5767)
E AndroidRuntime:   ... 8 more

错误原因:

Android 8.0 不再允许后台service直接通过startService方式去启动, 具体行为变更如下:

如果针对 Android 8.0 的应用尝试在不允许其创建后台服务的情况下使用 startService() 函数,则该函数将引发一个 IllegalStateException。 新的 Context.startForegroundService() 函数将启动一个前台服务。现在,即使应用在后台运行, 系统也允许其调用 Context.startForegroundService()。不过,应用必须在创建服务后的五秒内调用该服务的 startForeground() 函数。

解决方法:

1. 修改启动方式

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(intent);
} else {
    context.startService(intent);
}

2. 并且在service里再调用startForeground方法,不然就会出现ANR

context.startForeground(SERVICE_ID, builder.getNotification());

参考
https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten

猜你喜欢

转载自blog.csdn.net/u010784887/article/details/79675147