zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    如何降低客户流失率高的问题
    移动端时代如何进行营销革命
    EDM备忘录:触发式邮件订阅和退订功能介绍
    EDM博主笔记:EDM邮件营销的几个细节问题
    转载好文:如何进行基于情境数据的个性化EDM数据营销
    个性化EDM数据营销的三大提醒
    许可EDM营销是个长期过程
    内容营销三大实用法则(内含干货)-同样可运用在EDM数据营销中
    改善EDM数据营销的关键点
    《FS Book》: 如何让圣诞节邮件营销与众不同
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/3246019.html
Copyright © 2011-2022 走看看