Android O上Service适配方案

针对 Android 8.0 的应用尝试在不允许其创建后台服务的情况下使用  startService()  函数,则该函数将引发一个  IllegalStateException ,提示语如下:
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
新的 Context.startForegroundService() 函数将启动一个前台服务。现在,即使应用在后台运行,系统也允许其调用 Context.startForegroundService()。不过,应用必须在创建服务后的五秒内调用该服务的  startForeground()  函数,而且还有创建通知栏。
如果在5s内没有在onCreate方法中调用 startForeground() 函数,就会报ANR错误,当设备比较卡可能就ANR啦,提示语如:
05-08 21:52:25.685 2055 2117 I zygote64: libdebuggerd_client: done dumping process 3336
05-08 21:52:25.688 2055 2117 E ActivityManager: ANR in xxx
05-08 21:52:25.688 2055 2117 E ActivityManager: PID: 3589
05-08 21:52:25.688 2055 2117 E ActivityManager: Reason: Context.startForegroundService() did not then call Service.startForeground()
揣摩谷歌意图,一直在主推JobService,所以android8.0后最好还是用JobService,记得JobService继承的是Service哦,要新开线程执行耗时操作

猜你喜欢

转载自blog.csdn.net/u013795543/article/details/80371730