IntentService 是继承自service,源码发现:
①在oncreate()方法中创建名称为“IntentService”的HandlerThread的线程,并把线程的looper和ServiceHandler进行绑定。其中这个ServiceHandler是一个内部的Handler.
@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); }
private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); stopSelf(msg.arg1); } }
②通过onStartCommand()传递服务intent,依次插入到工作队列中,并逐个发送给onHandleIntent()。
onStartCommand()方法会调用onStart()方法。
@Override public void onStart(@Nullable Intent intent, int startId) { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent;//这个intent就是启动service的那个intent mServiceHandler.sendMessage(msg); }
③在mServiceHandler中的handleMessage()进行处理这个Message,而在handleMessage()中会调用onHandleIntent()方法,但是这个方法是抽象的,所以需要进行重写。
ps: 1 当多次调用 startService(Intent) 时(onStartCommand也会调用多次)其实并不会创建新的工作线程,只是把消息加入消息队列中等待执行,所以,多次启动 IntentService 会按顺序执行事件;
2 如果服务停止,会清除消息队列中的消息,后续的事件得不到执行。
3 Service需要主动调用stopSelft()来结束服务,而IntentService不需要(在所有intent被处理完后,系统会自动关闭服务)
④使用场景:线程任务需要按顺序、在后台执行的使用场景;由于所有的任务都在同一个Thread looper里面来做,所以不符合多个数据同时请求的场景。
HandlerThread是继承自Thread,说白了就是一个线程,不过在HandlerThread中封装了Looper
@Override public void run() { mTid = Process.myTid(); Looper.prepare(); synchronized (this) { mLooper = Looper.myLooper(); notifyAll(); } Process.setThreadPriority(mPriority); onLooperPrepared();//重写这个方法做线程的初始化操作 Looper.loop(); mTid = -1; }