1.问题关键日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
FATAL EXCEPTION: main
Process: com.iflytek.autofly.mediax, PID: 6260
java.lang.RuntimeException: Unable to create application com.iflytek.autofly.mediax.App: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.iflytek.autofly.mediax/cn.kuwo.service.MainService (has extras) }: app is in background uid UidRecord{d667c54 u10s1000 RCVR idle change:uncached procs:1 seq(0,0,0)}
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5925)
at android.app.ActivityThread.access$1100(ActivityThread.java:200)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.iflytek.autofly.mediax/cn.kuwo.service.MainService (has extras) }: app is in background uid UidRecord{d667c54 u10s1000 RCVR idle change:uncached procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
at android.app.ContextImpl.startService(ContextImpl.java:1532)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at cn.kuwo.service.MainService.connect(SourceFile:84)
at cn.kuwo.service.MainService.connect(SourceFile:63)
at cn.kuwo.unkeep.a.a.a(SourceFile:69)
at cn.kuwo.application.App.onCreate(SourceFile:107)
at com.iflytek.autofly.entadapter.cpsp.kuwo.KuWoAppProxy.onCreate(KuWoAppProxy.java:77)
at com.iflytek.autofly.ent.ENT.startMusicService(ENT.java:74)
at com.iflytek.autofly.mediax.App.onCreate(App.java:264)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5920)
... 8 more

2.日志分析

1
报java.lang.IllegalStateException: Not allowed to start service Intent xxxx app is in background uid UidRecord

Android 8.0 有以下调整:
Android 8.0 的应用尝试在不允许其创建后台服务的情况下使用 startService() 函数,则该函数将引发一个 IllegalStateException。

新的 Context.startForegroundService() 函数将启动一个前台服务。现在,即使应用在后台运行,系统也允许其调用 Context.startForegroundService()。

不过,应用必须在创建服务后的五秒内调用该服务的 startForeground() 函数。

3.解决方案

1
2
3
4
5
6
7
8
9
// 在启动服务的地方判断系统版本
import android.content.Intent;
import android.os.Build;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, ServedService.class));
} else {
context.startService(new Intent(context, ServedService.class));
}
1
2
3
4
5
6
7
8
9
10
// 在服务的内部类oncreate方法上也需要添加过滤
import android.app.Notification;

@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(1,new Notification());
}
}