為什么要用IntentService
IntentService 與 Service 相比的好處:一方面不需要自己去 new Thread 了;另一方面不需要考慮在什么時候關閉該 Service 了。
源碼解析
當IntentService第一次啟動,它的onCreate方法會被調用,該方法會創建一個HandlerThread,然后使用它的Looper來構造一個Handler對象mServiceHandler,這樣通過mServiceHandler發送的消息最終都會在HandlerThread中執行。
@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 ,Handy class for starting a new thread that has a looper。
* 因為HandlerThread是一個Thread,我們調用其start方法就會調用run方法,其中會通過 Looper.myLooper()獲取當前線程的looper對象
*/
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) {
/**
* use the provided {@link Looper} instead of the default one.
*/
super(looper);
}
@Override
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
/**
* 自己停止Service服務
*/
stopSelf(msg.arg1);
}
}
每次啟動IntentService,它的onStartCommand方法就會被調用一次,onStartCommand調用了onStart方法。
@Override
public void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
IntentService僅僅通過mServiceHandler發送了一條消息,這個消息會在HandlerThread中處理。mServiceHandler收到消息后,會將Intent對象傳遞給onHandleIntent方法處理。
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
/**
* use the provided {@link Looper} instead of the default one.
*/
super(looper);
}
@Override
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
/**
* 嘗試自己停止Service服務
*/
stopSelf(msg.arg1);
}
}
當onHandleIntent方法執行結束之后,IntentService會嘗試通過stopSelf(int startId)來嘗試停止服務。之所以不用stopSelf()來停止服務,是因為stopSelf()會立刻停止服務,而stopSelf(int startId)則會等待所有的消息都處理完畢才回終止服務。一般來說,stopSelf(int startId)在嘗試停止服務之前會判斷最近啟動服務的次數是否和startId相等,如果相等則立刻停止服務。
2. 為什么不建議通過 bindService() 啟動 IntentService?
@Override
public IBinder onBind(Intent intent) {
return null;
}
IntentService 源碼中的 onBind() 默認返回 null;不適合 bindService() 啟動服務,如果你執意要 bindService() 來啟動 IntentService,可能因為你想通過 Binder 或 Messenger 使得 IntentService 和 Activity 可以通信,這樣那么 onHandleIntent() 不會被回調,相當于在你使用 Service 而不是 IntentService。
3. 為什么多次啟動 IntentService 會順序執行事件,停止服務后,后續的事件得不到執行?
IntentService 中使用的 Handler、Looper、MessageQueue 機制把消息發送到線程中去執行的,所以多次啟動 IntentService 不會重新創建新的服務和新的線程,只是把消息加入消息隊列中等待執行,而如果服務停止,會清除消息隊列中的消息,后續的事件得不到執行。
@Override
public void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
@Override
public void onDestroy() {
mServiceLooper.quit();
}