IntentService源码分析

  因为service在默认情况下是在主线程执行的。所以为了方便,正如AsyncTask用来简化我们编写工作线程,android也提供了IntentService用来执行

长期运行的服务。

  IntentService is a base class for Services that handle asynchronous requests (expressed as Intent) 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.

 

1.源码

 1 public abstract class IntentService extends Service {
//Looper Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them;
        // 所以onCreate中使用HandlerThread的Looper来初始化这个成员变量。
2 private volatile Looper mServiceLooper; 3 private volatile ServiceHandler mServiceHandler; 4 private String mName; 5 private boolean mRedelivery; 6     //When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it
        // service默认是在UI线程上运行的。为了防止阻塞主线程,在onCreate中创建了HandlerThread。因为这里的Handler要处理HandlerThread的消息,所以在构造函数中传入HandlerThread的Looper
7 private final class ServiceHandler extends Handler { 8 9   public ServiceHandler(Looper looper) { 10   super(looper); 11    } 12 13    @Override 14   public void handleMessage(Message msg) { 15   onHandleIntent((Intent)msg.obj); //模板pattern--------调用我们自己要实现外部类的方法 16    stopSelf(msg.arg1); 17    } 18 } 19 20 21 public IntentService(String name) { 22 super(); 23 mName = name; 24 } 25 26 public void setIntentRedelivery(boolean enabled) { 27 mRedelivery = enabled; 28 } 29 30 @Override 31 public void onCreate() { 32 super.onCreate();
//Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes.
33 HandlerThread thread = new HandlerThread("IntentService[" + mName + "]"); 34 thread.start(); 35 36 mServiceLooper = thread.getLooper(); //用HandlerThread的Looper初始化IntentService成员变量Looper 37 mServiceHandler = new ServiceHandler(mServiceLooper); //用Looper初始化Handler 38 } 39 40 @Override 41 public void onStart(Intent intent, int startId) { 42 Message msg = mServiceHandler.obtainMessage(); 43 msg.arg1 = startId; 44 msg.obj = intent; 45 mServiceHandler.sendMessage(msg); 46 } 47 48 @Override 49 public int onStartCommand(Intent intent, int flags, int startId) { 50 onStart(intent, startId); 51 return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY; 52 } 53 54 @Override 55 public void onDestroy() { 56 mServiceLooper.quit(); 57 } 58 59 60 @Override 61 public IBinder onBind(Intent intent) { 62 return null; 63 } 64 65 protected abstract void onHandleIntent(Intent intent); 66 }

 

 

posted @ 2013-08-08 16:05  等风来。。  Views(270)  Comments(0Edit  收藏  举报
------------------------------------------------------------------------------------------------------------ --------------- 欢迎联系 x.guan.ling@gmail.com--------------- ------------------------------------------------------------------------------------------------------------