出错信息

E AndroidRuntime: FATAL EXCEPTION: mainE AndroidRuntime: Process: com.cjl.servicetest, PID: 4455E 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.lbe.parallel.service.KeyguardService.a(KeyguardService.java:80)E AndroidRuntime:   at com.lbe.parallel.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

更多相关文章

  1. android例子
  2. Android中多层Fragment嵌套,调用相册返回Uri无法显示图片的问题解
  3. 【转】Android获取用于操作数据库的SQLiteDatabase实例【学习记
  4. Android(安卓)Service Bind启动调用service方法
  5. Android简单例子(拨号服务调用)
  6. [Android]当Activity启动模式为singleTask时的生命周期
  7. Android中Linux睡眠唤醒流程
  8. 第五节cocos2dx的jni部分
  9. 箭头函数的基础使用

随机推荐

  1. android_tips
  2. android RSA和Java RSA加密不一致的坑
  3. 禁止android显示状态栏
  4. Android中文API(138) —— RemoteViews
  5. Android(安卓)SQLiteOpenHelper Sqlite数
  6. Android架构组件-Navigation的使用(一)
  7. android配置X86虚拟机
  8. Android兼容android7.0、及Android8.0以
  9. android中widgets的简单实现
  10. Android(安卓)EventBus使用,粘性事件post