串行java
public class LocalIntentService extends IntentService { public static final String TAG="LocalIntentService"; public LocalIntentService( ) { super(TAG); } @Override protected void onHandleIntent(Intent intent) { String task=intent.getStringExtra("task"); Log.e(TAG, "onHandleIntent: task:"+task ); } @Override public void onCreate() { super.onCreate(); Log.e(TAG, "onCreate: " ); } @Override public void onDestroy() { super.onDestroy(); Log.e(TAG, "onDestroy: " ); } }
<service android:name=".LocalIntentService"/>
findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent service=new Intent(MainActivity.this,LocalIntentService.class); service.putExtra("task","task1"); startService(service); service.putExtra("task","task2"); startService(service); service.putExtra("task","task3"); startService(service); service.putExtra("task","task4"); startService(service); } });
//IntentService第一次启动调用 public void onCreate() { super.onCreate(); //1. 建立一个HanlderThread HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); thread.start(); //2. 经过HanlderThread的Looper来构建Handler对象mServiceHandler mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper);
- HandlerThread会串行的取出任务而且执行,会调用ServiceHandler的handleMessage去处理任务。
- handlerMessage会去调用咱们自定义的onHandleIntent
- 任务执行完毕后经过stopSelf(startId)中止Service。
- 任务结束后,在onDestory()中会退出HandlerThread中Looper的循环。
//ServiceHandler接收并处理onStart()方法中发送的Msg private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { //1. 直接在onHandleIntent中处理(由子类实现) onHandleIntent((Intent)msg.obj); /**================================================= * 2. 尝试中止服务(会等待全部消息都处理完毕后,才会中止) * 不能采用stopSelf()——会当即中止服务 *================================================*/ stopSelf(msg.arg1); //会判断启动服务次数是否与startId相等 } } public void onDestroy() { mServiceLooper.quit(); }//销毁时会中止looper
- 采用stopSelf()——会当即中止服务
- 采用stopSelf(startId),会等全部消息所有处理完毕后,才会中止。
会判断启动服务次数是否与startId相等
- 调用stopSelf(startId)后。
- 任务所有执行完,会中止服务,而且回调onDestory()。调用Looper的quit()方法便可
- startService()->onStartCommand()->onStart()
- 经过HandlerThread的handler去发送消息。
- HandlerThread在处理任务时,会去调用onHandleIntent方法。
public abstract class IntentService extends Service { private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; ...省略... //IntentService每次启动都会调用 public int onStartCommand(Intent intent, int flags, int startId) { //3. 直接调用onStart onStart(intent, startId); return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY; } public void onStart(Intent intent, int startId) { //4. 经过mServiceHandler发送一个消息(该消息会在HanlderThread中处理) Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; msg.obj = intent; mServiceHandler.sendMessage(msg); } //2. 该Intent与startService(intent)中的Intent彻底一致 protected abstract void onHandleIntent(Intent intent); }
- IntentService经过发送消息的方式向HandlerThread请求执行任务
- HandlerThread中的looper是顺序处理消息,所以有多个后台任务时,都会按照外界发起的顺序排队执行
- 启动流程:onCreate()->onStartCommand()->onStart()
- 消息处理流程:ServiceHandler.handleMessage()->onHandleIntent()