Broadcast Receiver用于接收并处理广播通知,众所周知,onReceive(Context curContext, Intent broadcastMsg)方法用于接受通知,并在该方法体内处理消息,可是onReceive中代码的执行时间不要超过5s,不然会报ANR错误,致使程序异常。通常这种状况,会有两个方案:
1)在onReceive方法中开启子线程,作耗时操做android
2)在onReceive方法中启动新的Service,作耗时操做
bash
第一种方式很明显不可行,由于BroadCastReceiver的生命周期很短,一旦被回收后,onReceive方法中开启的线程就成了空线程,当手机资源紧张回收资源时,空线程会被系统回收,致使子线程中的任务并不能顺利执行完毕就被杀死。app
通常咱们会用第二种方式,开启一个服务,可是咱们知道servce也不能作耗时操做,难道咱们在service再开启一个子线程吗?Google提供了IntentService类,帮咱们作好了一切,咱们只须要使用IntentSerivce便可,他内部帮咱们开启了子线程,咱们只须要重写onHandleIntent方法,在onHandleIntent方法中执行耗时操做。其实IntentService是使用了HandlerThread实现子线程耗时操做,若是对HandlerThread不熟悉,能够看上篇详细的介绍。
less
1)继承IntentService异步
public class MyService extends IntentService 复制代码
2)重写onHandleIntent()方法ide
@Override
protected void onHandleIntent(Intent intent) {
//执行耗时操做
}复制代码
所有代码以下所示:oop
/**
* 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的源码。
学习
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, * {@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. * * <p>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);
}复制代码
代码很是少,咱们开始分析。ui
1)首先,当启动IntentService的时候,会调用他的onCreate方法,若是看了上篇HandlerThread的使用代码,你应该会很是眼熟,他其实就是在建立HandlerThread,并经过建立出来的HandlerThread获取其中的Looper,经过Looper建立出mServiceHandler对象,由于Looper是在子线程中建立的,咱们用该Looper对象建立出来的Handler天然持有异步线程的Looper,全部很正常,Hander的handleMessage方法能够执行耗时的异步操做。(若是仍是不明白,能够详细看上篇,对于HandlerThread的介绍)。this
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关闭,整个流程结束。
------------------------------------------------------------------------------------------------
若有错误欢迎指出来,一块儿学习。