android点滴(28)之IntentService

IntentService

 

IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time. 

 

从Android官方文档可以看出IntentService是可以处理异步请求的。那它是如何处理的呢?这里首先要说一下,Android的消息机制。我们知道Android的Activity都有一个默认的消息循环队列,IntentService就利用了消息的方式来处理异步请求的。 

 

 1 private volatile Looper mServiceLooper;
 2 private volatile ServiceHandler mServiceHandler;
 3 
 4 
 5 private final class ServiceHandler extends Handler {
 6         public ServiceHandler(Looper looper) {
 7             super(looper);
 8         }
 9 
10         @Override
11         public void handleMessage(Message msg) {
12             onHandleIntent((Intent)msg.obj);
13             stopSelf(msg.arg1);
14         }

15 }  


 从上面IntentService的源代码可以看出,它有Looper和一个Handler对象。Looper是消息循环,Handler是处理逻辑。

 

 1 @Override
 2     public void onCreate() {
 3         // TODO: It would be nice to have an option to hold a partial wakelock
 4         // during processing, and to have a static startService(Context, Intent)
 5         // method that would launch the service & hand off a wakelock.
 6 
 7         super.onCreate();
 8         HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
 9         thread.start();
10 
11         mServiceLooper = thread.getLooper();
12         mServiceHandler = new ServiceHandler(mServiceLooper);

13     } 

  


 在IntentService中的onCreate中启动了一个叫做HandlerThread的线程,HandlerThread是一个拥有自己的Looper的Handler,关于这个类请查看http://www.cnblogs.com/cody1988/archive/2012/06/20/2556495.html 。IntentService中的Looper与HandlerThread的Looper是同一个。

 

当IntentService接收到一个Intent时,就会发送一个消息,在 ServiceHandler中对消息进行处理,我们看到ServiceHandler中调用了onHandleIntent方法,这个方法是一个抽象方法,我们要重载这个方法,来添加自己的处理逻辑。


    public void onStart(Intent intent, int startId) {
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        msg.obj = intent;
        mServiceHandler.sendMessage(msg);
    }

    /**
     * You should not override this method for your IntentService. Instead,
     * override {
@link #onHandleIntent}, which the system calls when the IntentService
     * receives a start request.
     * 
@see android.app.Service#onStartCommand
     
*/
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        onStart(intent, startId);
        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
    }

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2012-07-04 13:29  誑逩の蝸犇  阅读(296)  评论(0编辑  收藏  举报