IntentService是可处理异步请求的Service抽象子类,必须实现onHandleIntent
,这个回调方法是在工作线程执行的;
- IntentService主线程和工作线程之间的内部通信也是通过Handler来实现的,是单一工作线程,顺序执行;
- 调用者通过调用
android.content.Context#startService(Intent)
来启动发送请求,service一旦启动,则在工作线程顺序处理每一个Intent,任务执行完就会自己结束
Service创建后,就会创建一个工作线程
@Override
public void onCreate() {
// TODO: It would be nice to have an option to hold a partial wakelock
// during processing, and to have a static startService(Context, Intent)
// method that would launch the service & hand off a wakelock.
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public void