IntentService源码讲解

 

 

1. 前言

Broadcast Receiver用于接收并处理广播通知,众所周知,onReceive(Context curContext, Intent broadcastMsg)方法用于接受通知,并在该方法体内处理消息,但是onReceive中代码的执行时间不要超过5s,否则会报ANR错误,导致程序异常。一般这种情况,会有两个方案:
1)在onReceive方法中开启子线程,做耗时操作

2)在onReceive方法中启动新的Service,做耗时操作

 

第一种方式很明显不可行,因为BroadCastReceiver的生命周期很短,一旦被回收后,onReceive方法中开启的线程就成了空线程,当手机资源紧张回收资源时,空线程会被系统回收,导致子线程中的任务并不能顺利执行完毕就被杀死。

一般我们会用第二种方式,开启一个服务,但是我们知道servce也不能做耗时操作,难道我们在service再开启一个子线程吗?Google提供了IntentService类,帮我们做好了一切,我们只需要使用IntentSerivce即可,他内部帮我们开启了子线程,我们只需要重写onHandleIntent方法,在onHandleIntent方法中执行耗时操作。其实IntentService是使用了HandlerThread实现子线程耗时操作,如果对HandlerThread不熟悉,可以看上篇详细的介绍。

 

2.使用方法

1)继承IntentService

public class MyService extends IntentService 

2)重写onHandleIntent()方法

 @Override
    protected void onHandleIntent(Intent intent) {
        //执行耗时操作
    }

全部代码如下所示:

/**
 * Created by Administrator on 2018/5/31.
 */
public class MyService extends IntentService {
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public MyService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        //执行耗时操作
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }
}

我们知道,当启动service后,会执行oncreate()->onStartCommand->onStart()方法,但是为什么我们的耗时操作却写在onHandlleIntent方法中,他内部是如何处理的呢,我们看下IntentService的源码。

 

3.源码讲解

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.
     *
     * 

If enabled is true, * {@link #onStartCommand(Intent, int, int)} will return * {@link Service#START_REDELIVER_INTENT}, so if this process dies before * {@link #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. * *

If enabled is false (the default), * {@link #onStartCommand(Intent, int, int)} will return * {@link 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 {@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; } @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. * @see 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 {@link #stopSelf}. * * @param intent The value passed to {@link * android.content.Context#startService(Intent)}. */ protected abstract void onHandleIntent(Intent intent); }

代码非常少,我们开始分析。

1)首先,当启动IntentService的时候,会调用他的onCreate方法,如果看了上篇HandlerThread的使用代码,你应该会非常眼熟,他其实就是在创建HandlerThread,并通过创建出来的HandlerThread获取其中的Looper,通过Looper创建出mServiceHandler对象,因为Looper是在子线程中创建的,我们用该Looper对象创建出来的Handler自然持有异步线程的Looper,所有很正常,Hander的handleMessage方法可以执行耗时的异步操作。(如果还是不明白,可以详细看上篇,对于HandlerThread的介绍)。

2)onCreate方法执行完毕后,会执行onStartCommand方法,onStartCommand又会调用onStart方法,我们看下onStart方法体,里面其实就做了一件事,发送handler消息。接下来,肯定是看该mServiceHandler类的handlerMessage方法在哪里。mServiceHandler是内部类,handlerMessage方法里面调用了onHandleIntent方法,因为handlerMessage是在异步线程中,所以onHandleIntent方法也在异步线程中,并且onHandleIntent是个抽象方法,我们只要重写onHandleIntent方法体,即可实现异步线程处理耗时操作。

3)在onHandleIntent方法后面,我们看到,会再调用stopSelf(msg.arg1)方法,stopSelf(int startId)会等待所有消息都处理完后才终止服务,也就是说异步线程结束后,服务会自己关闭。

4)如果你多次startService,这时候,Service的生命周期是这样的:oncreate()->onStartCommand->onStart()>onStartCommand->onStart()>onStartCommand->onStart()...->onHandlerIntent()->onHandlerIntent()->onHandlerIntent(),很容易理解,这就是Service的生命周期,至于为什么onHandlerIntent方法为什么会多次被调用,因为每次调用onStart(),都会发送一次handler消息,而多次调用会,message会按照时间顺序存储在messageQueue单向链表中,Looper对象会不停的取出Message处理,即回调HandlerMessage方法,从而执行到onHandlerIntent方法,这边不清楚的,可以看前面源码角度讲解Android消息处理机制(Handler、Looper、MessageQueue与Message)。当多个消息都处理结束后,stopSelf(msg.arg1)方法执行后,service关闭,整个流程结束。


欢迎一起交流讨论
群号:469890293


关注我的公众号,更多优质文章将通过公众号推送。
微信扫一扫下方二维码即可关注:

IntentService源码讲解_第1张图片

 

你可能感兴趣的:(android源码阅读)