【Android】IntentService原理分析

个人博客:
http://www.milovetingting.cn

IntentService是一个异步处理请求的服务,通过Context#startService(Intent)可以将请求发送给IntentService,IntentService在工作线程中依次串行处理每一个Intent,当处理完所有请求后,IntentService会自动停止。

在IntentService内部是通过HandlerThread来切换线程和处理消息的。

当IntentService首次启动时,会调用onCreate()方法:

@Override
public void onCreate() {
    super.onCreate();
    //创建HandlerThread
    HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
    //启动线程
    thread.start();

    //从HandlerThread中获取looper
    mServiceLooper = thread.getLooper();
    //实例化Handler
    mServiceHandler = new ServiceHandler(mServiceLooper);
}

在onCreate()方法中,首先创建了HandlerThread,然后启动它。然后从创建的thread中获取looper,并实例化Handler。

onStartCommand()方法:

/**
 * 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(@Nullable Intent intent, int flags, int startId) {
    onStart(intent, startId);
    return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}

onStartCommand()方法,在自定义类继承自IntentService时,不要去重写,而应该重写onHandleIntent()方法。在onStartCommand()方法中,调用了onStart()方法,将intent传入Message,并发送出去。

onStart()方法:

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

在onStart()方法中,将intent信息打包到Message中,并发送到消息队列。

Message消息的处理:

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);
    }
}

onStart()方法将intent传入了Message,最终在ServiceHandler的handleMessage()方法处理,在handleMessage()方法中,回调了onHandleIntent()方法,这个方法是需要我们重写的,由于ServiceHandler是运行在子线程中,所以onHandleIntent()的执行也会在子线程中。当依次执行完任务后,调用了stopSelf(startId)方法停止Service。

stopSelf(startId)方法,只有当startId和最后启动Service时的startId一致时,才会停止服务,所以如果还有任务没有执行完成,则不会成功停止服务。

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

在onDestory()方法中,调用Looper的quit()方法,退出消息循环。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是IntentService? 一个带有工作线程的,并且具有任务完成会自动停止功能的Service 一.Int...
    Natchi阅读 179评论 0 0
  • IntentService继承于Service,它的特点是什么呢?就跟龙叔小程序的特点一样——“用完即走”。 用过...
    俗人浮生阅读 4,926评论 0 5
  • 前言:本文所写的是博主的个人见解,如有错误或者不恰当之处,欢迎私信博主,加以改正!原文链接,demo链接 Serv...
    PassersHowe阅读 1,451评论 0 5
  • 在分析IntentService之前 需要了解下Android四大组件之一的Service(服务)同时为了更好的理...
    明朗__阅读 724评论 0 5
  • 不管学什么知识点最好从其基本用法开始,然后在深入到源码层学习会比较容易理解源码带给我们的思想。所以咱们先来看看In...
    laogui阅读 788评论 1 4