本文內容基于《Android開發藝術探索》,強烈推薦,值得一看。
IntentService是繼承自Service的抽象類,內部封裝了HandlerThread和Handler,可以用來執行后臺耗時的任務。它的優點是優先級高不容易被殺死,IntentService可以處理多個任務,只不過是一個接著一個的順序來處理的,當任務完成后會自動停止,不需要主動調用stopSelft()來結束服務。onHandlerIntent是它的一個抽象方法,它可以從Intent中傳入的參數來區分并執行任務。
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中添加了兩個按鈕,對應operate1、operate2方法,operate1是單任務,operate2是多任務,運行一下看看具體的log輸出,先點擊operate1看看單任務的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
再執行operate2看看多任務的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任務結束后會自動銷毀,多任務的時候是一個一個順序執行的,上一個任務的onHandleIntent執行完成才會調用下一個任務的onHandleIntent。
2.工作原理
onCreate()方法如下所示
public void onCreate() {
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
IntentService第一次啟動時會調用onCreate方法,創建一個HandlerThread(不熟悉的同學可以參考Android線程—HandlerThread的使用及原理)對象,并開啟HandlerThread線程,然后使用HandlerThread的Looper對象初始化mServiceHandler對象,通過mServiceHandler發送消息在HandlerThread中執行操作,所以雖然IntentService也是Service但是可以執行耗時操作。每次啟動IntentService都會調用onStartCommand()方法,onStartCommand方法會調用onStart方法
onStart方法如下所示
@Overridepublic void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
我們可以發現onStart方法中只是調用mServiceHandler發送了一個消息,交給HandlerThread處理,這里傳入的參數intent即為外界通過startIntent(intent)傳入的intent,startId是當前IntentService的啟動次數,第一次為1、第二次為2... 。
接下來我們看一下ServiceHandler的實現
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的一個抽象方法,我們在子類中實現具體功能,該方法執行完以后會嘗試調用stopSelf(startId)方法來終止服務。(stopSelf(int startId)與stopSelf()的區別:stopSelf()直接停止;stopSelf(startId)只有在startId與最后啟動該service時生成的startId相等時才會執行停止服務,即所有消息都處理完成)。因為IntentService每啟動一次都會發送一個消息請求HandlerThread執行,Looper是順序處理消息的,這就意味著IntentService執行多個任務時也是順序執行的。
本人技術有限,歡迎指正,謝謝!