源码解析IntentService的好处
2013-09-26 17:57
453 查看
文章摘自:/article/2179313.html
源码:
它是将发来的Intent放到一个新线程中去处理的。对于异步的startService请求,IntentService的这个新线程会处理完第一个,再处理第二个。
这样的设计和Service有关。
先来说说Service,它有两点要说明:
1、Service不是一个进程,它和应用程序在同一个进程中;
2、Service也不是一个线程,它运行在应用程序的主线程中。
因此,一般用Service时有耗时的操作,都会将这些操作放到新启的线程中。这样的话,需要自己在Service中创建线程,而IntentService就帮我们做了这项工作。
因为大部分Service的操作都需要另启线程,也就是说大部分Service都可以用IntentService来代替。这就是IntentService的作用。
源码:
public abstract class IntentService extends Service { private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; private String mName; private boolean mRedelivery; 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, * {<A href="http://my.oschina.net/link1212" rel=nofollow target=_blank>@link</A> #onStartCommand(Intent, int, int)} will return * {<A href="http://my.oschina.net/link1212" rel=nofollow target=_blank>@link</A> Service#START_REDELIVER_INTENT}, so if this process dies before * {<A href="http://my.oschina.net/link1212" rel=nofollow target=_blank>@link</A> #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), * {<A href="http://my.oschina.net/link1212" rel=nofollow target=_blank>@link</A> #onStartCommand(Intent, int, int)} will return * {<A href="http://my.oschina.net/link1212" rel=nofollow target=_blank>@link</A> 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); } /** * You should not override this method for your IntentService. Instead, * override {<A href="http://my.oschina.net/link1212" rel=nofollow target=_blank>@link</A> #onHandleIntent}, which the system calls when the IntentService * receives a start request. * <A href="http://my.oschina.net/u/244147" rel=nofollow target=_blank>@see</A> 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; } @Override public void onDestroy() { mServiceLooper.quit(); } /** * Unless you provide binding for your service, you don't need to implement this * method, because the default implementation returns null. * <A href="http://my.oschina.net/u/244147" rel=nofollow target=_blank>@see</A> android.app.Service#onBind */ @Override public IBinder onBind(Intent intent) { return null; } /** * This method is invoked on the worker thread with a request to process. * Only one Intent is processed at a time, but the processing happens on a * worker thread that runs independently from other application logic. * So, if this code takes a long time, it will hold up other requests to * the same IntentService, but it will not hold up anything else. * When all requests have been handled, the IntentService stops itself, * so you should not call {<A href="http://my.oschina.net/link1212" rel=nofollow target=_blank>@link</A> #stopSelf}. * * @param intent The value passed to {<A href="http://my.oschina.net/link1212" rel=nofollow target=_blank>@link</A> * android.content.Context#startService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); }
它是将发来的Intent放到一个新线程中去处理的。对于异步的startService请求,IntentService的这个新线程会处理完第一个,再处理第二个。
这样的设计和Service有关。
先来说说Service,它有两点要说明:
1、Service不是一个进程,它和应用程序在同一个进程中;
2、Service也不是一个线程,它运行在应用程序的主线程中。
因此,一般用Service时有耗时的操作,都会将这些操作放到新启的线程中。这样的话,需要自己在Service中创建线程,而IntentService就帮我们做了这项工作。
因为大部分Service的操作都需要另启线程,也就是说大部分Service都可以用IntentService来代替。这就是IntentService的作用。
相关文章推荐
- Android IntentService的使用与源码解析
- IntentService源码解析
- IntentService从源码解析
- Android 进阶16:IntentService 使用及源码解析
- IntentService的简单使用和源码解析
- Android源码基础解析之IntentService
- Android中IntentService的使用及其源码解析
- Android IntentService使用全面介绍及源码解析
- IntentService源码解析与HandlerThread在Android框架上的使用
- Android中IntentService的使用及其源码解析
- Android IntentService的使用详情和源码解析
- IntentService使用及源码解析
- 【转载】Android IntentService使用全面介绍及源码解析
- IntentService源码解析
- Android多线程全面解析:IntentService用法&源码
- Android多线程(四)IntentService源码原理解析
- Android多线程全面解析:IntentService用法&源码
- IntentService 源码解析
- 源码解析---IntentService
- Android IntentService使用全面介绍及源码解析