最近App出现了一个线上crash,日志如下:
java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.xxx.xx/.core.service.NotifyJobService }: app is in background uid UidRecord{6dd5849 u0a172 TPSL idle change:idle|cached procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1628)
at android.app.ContextImpl.startService(ContextImpl.java:1569)
at android.content.ContextWrapper.startService(ContextWrapper.java:675)
at com.xxx.xx.application.MainApp.onCreate(Unknown Source:385)
at com.baidu.protect.A.a(Native Method)
at com.baidu.protect.StubApplication.onCreate(StubApplication.java:296)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6273) ... 8 more
具体原因是因为从Android 8.0开始禁止应用在后台运行时创建Service。
网上的解决方案大致有以下几种:
1.通过Context.startForegroundService()方式启动一个前台Service,前台Service的启动没有受到限制。
2.集成Google Firebase Messaging。
3.使用JobIntentService。
其中方案一最简单,并且是国内大多数blog博主推荐的解决方案,但是考虑到项目需求,启动一个前台Service实在是不合适。
方案二、三普遍被国外开发者所采用,但是Google Firebase毕竟在国内使用会有诸多限制,于是个人决定采用方案三来解决。
JobIntentService
JobIntentService是Android 8.0 新加入的类,它也是继承自Service,根据官方的解释:
Helper for processing work that has been enqueued for a job/service. When running on Android O or later, the work will be dispatched as a job via JobScheduler.enqueue. When running on older versions of the platform, it will use Context.startService.
大致是说JobIntentService用于执行加入到队列中的任务。对Android 8.0及以上的系统,JobIntentService的任务将被分发到JobScheduler.enqueue执行,对于8.0以下的系统,任务仍旧会使用Context.startService执行。
JobIntentService具体使用起来非常简单,它已经封装了大量的内部逻辑,只需要调用enqueue()就可以了。
使用示例:
1.在Manifest中声名Permission:
<uses-permission android:name="android.permission.WAKE_LOCK" />
2.在Manifest中声名Service:
<service android:name=".YourService" android:permission="android.permission.BIND_JOB_SERVICE" />
3.实现JobIntentService类:
public class YourService extends JobIntentService {
public static final int JOB_ID = 1;
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, YourService.class, JOB_ID, work);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
// 具体逻辑
}
}
4.需要调用的地方:
YourService.enqueueWork(context, new Intent());
我们可以看到,JobIntentService的使用相比JobService简化了很多,开发者甚至不需要关心JobIntentService的生命周期,不需要startService()方法,也就避免了开头中的crash问题,通过静态方法就可以启动,可以说是非常友好了。
网友评论