Service作為Android四大組件之一在項(xiàng)目開發(fā)中至關(guān)重要,一些耗時(shí)的業(yè)務(wù)都會(huì)用到Service,但是因?yàn)镾ervice是在主線程當(dāng)中的,所以要想在Service中做一些耗時(shí)的操作必須要自己創(chuàng)建Thread,還好Android提供了另外一個(gè)類就是我們要講的IntentService,IntentService是Service的子類,下面就讓我們通過源碼來看看它內(nèi)部的實(shí)現(xiàn),我們先來看看它的onCreate方法。
onCreate方法
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);
}
首先創(chuàng)建了一個(gè)HandlerThread,不了解HandlerThread的可以看看這篇文章(http://www.lxweimin.com/p/d1e0bb996601)
啟動(dòng)線程,創(chuàng)建ServiceHandler,通過onCreate方法可以看出它內(nèi)部封裝了一個(gè)
Handler,Looper,Thread的消息處理邏輯。
onStart方法
public void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
通過mServiceHandler發(fā)送了一條消息,然后回到handleMessage執(zhí)行邏輯
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
onHandleIntent就是我們要重寫的方法,這里處理我們的業(yè)務(wù)邏輯,stopSelf方法銷毀IntentService,如果這條消息是最后一條消息就銷毀,否則不銷毀,我們通過代碼來驗(yàn)證下
public class TestService extends IntentService {
public static final String TAG = "TestService";
public TestService() {
super("st");
}
@Override
protected void onHandleIntent(Intent intent) {
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand:" + startId);
return super.onStartCommand(intent, flags,startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
}
}
通過startService啟動(dòng)
startService(new Intent(EleActivity.this, TestService.class));
我們來看看打印結(jié)果
01-24 13:42:53.597 3172-3172/com.fear I/TestService: onCreate
01-24 13:42:53.598 3172-3172/com.fear I/TestService: onStartCommand:1
01-24 13:42:53.600 3172-3172/com.fear I/TestService: onDestroy
我們調(diào)用了onStartCommand方法一次發(fā)送一條消息,消息處理完銷毀自己。
那我們發(fā)送5條消息會(huì)是什么有樣子呢?
for(int i = 0; i < 5; i++)
{
startService(new Intent(EleActivity.this, TestService.class));
}
因?yàn)镮ntentService的onCreate只會(huì)調(diào)用一次,如果啟動(dòng)5次是會(huì)調(diào)用5次onStartCommand方法,我們來看看打印結(jié)果
01-24 13:45:02.535 6363-6363/com.fear I/TestService: onCreate
01-24 13:45:02.536 6363-6363/com.fear I/TestService: onStartCommand:1
01-24 13:45:02.536 6363-6363/com.fear I/TestService: onStartCommand:2
01-24 13:45:02.536 6363-6363/com.fear I/TestService: onStartCommand:3
01-24 13:45:02.537 6363-6363/com.fear I/TestService: onStartCommand:4
01-24 13:45:02.537 6363-6363/com.fear I/TestService: onStartCommand:5
01-24 13:45:02.542 6363-6363/com.fear I/TestService: onDestroy
可以看到調(diào)用了5次onStartCommand,發(fā)送了5條消息,消息都處理完,然后銷毀自己。
總結(jié)
IntentService是Service子類,重寫了onCreate方法和onStart方法,內(nèi)部封裝了Handler,Looper,Thread來處理消息,開發(fā)者可以通過復(fù)寫onHandleIntent來處理自己的業(yè)務(wù)邏輯。