Android中的Service是用於后台服務的,當應用程序被掛到后台的時候,問了保證應用某些組件仍然可以工作而引入了Service這個概念,那么這里面要強調的是Service不是獨立的進程,也不是獨立的線程,它是依賴於應用程序的主線程的,也就是說,在更多時候不建議在Service中編寫耗時的邏輯和操作,否則會引起ANR。
那么我們當我們編寫的耗時邏輯,不得不被service來管理的時候,就需要引入IntentService,IntentService是繼承Service的,那么它包含了Service的全部特性,當然也包含service的生命周期,那么與service不同的是,IntentService在執行onCreate操作的時候,內部開了一個線程,去你執行你的耗時操作。
這里我 需要解釋以下幾個方法,也許大家都已經很清楚了
Service中提供了一個方法:
- public int onStartCommand(Intent intent, int flags, int startId) {
- onStart(intent, startId);
- return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
- }
這個方法的具體含義是,當你的需要這個service啟動的時候,或者調用這個servcie的時候,那么這個方法首先是要被回調的。
同時IntentService中提供了這么一個方法:
- protected abstract void onHandleIntent(Intent intent);
這是一個抽象方法,也就是說具體的實現需要被延伸到子類。
子類的聲明:
- public class ChargeService extends IntentService
上面提到過IntentService是繼承Service的,那么這個子類也肯定繼承service,那么onHandleIntent()方法是什么時候被調用的呢?讓我們具體看IntentService的內部實現:
- 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);
- }
- }
- /**
- * Creates an IntentService. Invoked by your subclass's constructor.
- *
- * @param name Used to name the worker thread, important only for debugging.
- */
- public IntentService(String name) {
- super();
- mName = name;
- }
- /**
- * Sets intent redelivery preferences. Usually called from the constructor
- * with your preferred semantics.
- *
- * <p>If enabled is true,
- * {@link #onStartCommand(Intent, int, int)} will return
- * {@link Service#START_REDELIVER_INTENT}, so if this process dies before
- * {@link #onHandleIntent(Intent)} returns, the process will be restarted
- * and the intent redelivered. If multiple Intents have been sent, only
- * the most recent one is guaranteed to be redelivered.
- *
- * <p>If enabled is false (the default),
- * {@link #onStartCommand(Intent, int, int)} will return
- * {@link Service#START_NOT_STICKY}, and if the process dies, the Intent
- * dies along with it.
- */
- public void setIntentRedelivery(boolean enabled) {
- mRedelivery = enabled;
- }
- @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 thread = new HandlerThread("IntentService[" + mName + "]");
- thread.start();
- mServiceLooper = thread.getLooper();
- mServiceHandler = new ServiceHandler(mServiceLooper);
- }
- @Override
- public void onStart(Intent intent, int startId) {
- Message msg = mServiceHandler.obtainMessage();
- msg.arg1 = startId;
- msg.obj = intent;
- mServiceHandler.sendMessage(msg);
- }
在這里我們可以清楚的看到其實IntentService在執行onCreate的方法的時候,其實開了一個線程HandlerThread,並獲得了當前線程隊列管理的looper,並且在onStart的時候,把消息置入了消息隊列,
- @Override
- public void handleMessage(Message msg) {
- onHandleIntent((Intent)msg.obj);
- stopSelf(msg.arg1);
- }
在消息被handler接受並且回調的時候,執行了onHandlerIntent方法,該方法的實現是子類去做的。
結論:
IntentService是通過Handler looper message的方式實現了一個多線程的操作,同時耗時操作也可以被這個線程管理和執行,同時不會產生ANR的情況。