本文內(nèi)容基于《Android開發(fā)藝術(shù)探索》,強(qiáng)烈推薦,值得一看。
IntentService是繼承自Service的抽象類服爷,內(nèi)部封裝了HandlerThread和Handler,可以用來(lái)執(zhí)行后臺(tái)耗時(shí)的任務(wù)获诈。它的優(yōu)點(diǎn)是優(yōu)先級(jí)高不容易被殺死仍源,IntentService可以處理多個(gè)任務(wù),只不過(guò)是一個(gè)接著一個(gè)的順序來(lái)處理的舔涎,當(dāng)任務(wù)完成后會(huì)自動(dòng)停止笼踩,不需要主動(dòng)調(diào)用stopSelft()來(lái)結(jié)束服務(wù)。onHandlerIntent是它的一個(gè)抽象方法亡嫌,它可以從Intent中傳入的參數(shù)來(lái)區(qū)分并執(zhí)行任務(wù)嚎于。
1.使用方法
1).MyIntentService.java
public class MyIntentService extends IntentService {
private static final String TAG = MyIntentService.class.getSimpleName();
public static final int OPERATION_CODE_1 = 1;
public static final int OPERATION_CODE_2 = 2;
public MyIntentService() {
super("MyIntentService");
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "onBind");
return super.onBind(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand * " + startId);
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.e(TAG, "onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
}
@Override
protected void onHandleIntent(Intent intent) {
Log.e(TAG, "onHandleIntent");
int operateCode = intent.getIntExtra("operateCode", 0);
switch (operateCode) {
case OPERATION_CODE_1:
Log.e(TAG, "OPERATION_CODE_1");
break;
case OPERATION_CODE_2:
Log.e(TAG, "OPERATION_CODE_2");
break;
default:
break;
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2).MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void operate1(View view) {
Intent operateIntent = new Intent(this, MyIntentService.class);
operateIntent.putExtra("operateCode", 1);
startService(operateIntent);
}
public void operate2(View view) {
Intent operateIntent1 = new Intent(this, MyIntentService.class);
operateIntent1.putExtra("operateCode", 1);
startService(operateIntent1);
Intent operateIntent2 = new Intent(this, MyIntentService.class);
operateIntent2.putExtra("operateCode", 2);
startService(operateIntent2);
}
}
在activity_main中添加了兩個(gè)按鈕,對(duì)應(yīng)operate1挟冠、operate2方法于购,operate1是單任務(wù),operate2是多任務(wù)知染,運(yùn)行一下看看具體的log輸出肋僧,先點(diǎn)擊operate1看看單任務(wù)的log輸出:
03-17 20:43:10.070 19856-19856/? E/MyIntentService: onCreate
03-17 20:43:10.072 19856-19856/? E/MyIntentService: onStartCommand * 1
03-17 20:43:10.073 19856-20279/? E/MyIntentService: onHandleIntent
03-17 20:43:10.073 19856-20279/? E/MyIntentService: OPERATION_CODE_1
03-17 20:43:16.076 19856-19856/? E/MyIntentService: onDestroy
再執(zhí)行operate2看看多任務(wù)的log輸出:
03-17 20:46:55.452 19856-19856/? E/MyIntentService: onCreate
03-17 20:46:55.453 19856-19856/? E/MyIntentService: onStartCommand * 1
03-17 20:46:55.453 19856-19856/? E/MyIntentService: onStartCommand * 2
03-17 20:46:55.453 19856-20991/? E/MyIntentService: onHandleIntent
03-17 20:46:55.453 19856-20991/? E/MyIntentService: OPERATION_CODE_1
03-17 20:47:01.453 19856-20991/? E/MyIntentService: onHandleIntent
03-17 20:47:01.453 19856-20991/? E/MyIntentService: OPERATION_CODE_2
03-17 20:47:07.456 19856-19856/? E/MyIntentService: onDestroy
從輸入的日志中我們可以了解到,IntentService任務(wù)結(jié)束后會(huì)自動(dòng)銷毀控淡,多任務(wù)的時(shí)候是一個(gè)一個(gè)順序執(zhí)行的嫌吠,上一個(gè)任務(wù)的onHandleIntent執(zhí)行完成才會(huì)調(diào)用下一個(gè)任務(wù)的onHandleIntent。
2.工作原理
onCreate()方法如下所示
public void onCreate() {
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
IntentService第一次啟動(dòng)時(shí)會(huì)調(diào)用onCreate方法掺炭,創(chuàng)建一個(gè)HandlerThread(不熟悉的同學(xué)可以參考Android線程—HandlerThread的使用及原理)對(duì)象居兆,并開啟HandlerThread線程,然后使用HandlerThread的Looper對(duì)象初始化mServiceHandler對(duì)象竹伸,通過(guò)mServiceHandler發(fā)送消息在HandlerThread中執(zhí)行操作,所以雖然IntentService也是Service但是可以執(zhí)行耗時(shí)操作簇宽。每次啟動(dòng)IntentService都會(huì)調(diào)用onStartCommand()方法勋篓,onStartCommand方法會(huì)調(diào)用onStart方法
onStart方法如下所示
@Overridepublic void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
我們可以發(fā)現(xiàn)onStart方法中只是調(diào)用mServiceHandler發(fā)送了一個(gè)消息,交給HandlerThread處理魏割,這里傳入的參數(shù)intent即為外界通過(guò)startIntent(intent)傳入的intent譬嚣,startId是當(dāng)前IntentService的啟動(dòng)次數(shù),第一次為1钞它、第二次為2... 拜银。
接下來(lái)我們看一下ServiceHandler的實(shí)現(xiàn)
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);
}
}
onHandleIntent的一個(gè)抽象方法殊鞭,我們?cè)谧宇愔袑?shí)現(xiàn)具體功能,該方法執(zhí)行完以后會(huì)嘗試調(diào)用stopSelf(startId)方法來(lái)終止服務(wù)尼桶。(stopSelf(int startId)與stopSelf()的區(qū)別:stopSelf()直接停止操灿;stopSelf(startId)只有在startId與最后啟動(dòng)該service時(shí)生成的startId相等時(shí)才會(huì)執(zhí)行停止服務(wù),即所有消息都處理完成)泵督。因?yàn)镮ntentService每啟動(dòng)一次都會(huì)發(fā)送一個(gè)消息請(qǐng)求HandlerThread執(zhí)行趾盐,Looper是順序處理消息的,這就意味著IntentService執(zhí)行多個(gè)任務(wù)時(shí)也是順序執(zhí)行的小腊。
本人技術(shù)有限救鲤,歡迎指正,謝謝秩冈!