您的位置:首页 > 其它

IntentService源码分析

2013-08-08 16:05 337 查看
  因为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.源码

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来初始化这个成员变量。
private volatile Looper mServiceLooper;
private volatile ServiceHandler mServiceHandler;
private String mName;
private boolean mRedelivery;
    //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
private final class ServiceHandler extends Handler {

  public ServiceHandler(Looper looper) {
  super(looper);
   }

   @Override
  public void handleMessage(Message msg) {
  onHandleIntent((Intent)msg.obj);  //模板pattern--------调用我们自己要实现外部类的方法
   stopSelf(msg.arg1);
   }
}

public IntentService(String name) {
super();
mName = name;
}

public void setIntentRedelivery(boolean enabled) {
mRedelivery = enabled;
}

@Override
public void onCreate() {
super.onCreate();
//Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes.
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();

mServiceLooper = thread.getLooper();                          //用HandlerThread的Looper初始化IntentService成员变量Looper
mServiceHandler = new ServiceHandler(mServiceLooper);         //用Looper初始化Handler
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}

@Override
public void onDestroy() {
mServiceLooper.quit();
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

protected abstract void onHandleIntent(Intent intent);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: