美文网首页
IntentService 源码解析

IntentService 源码解析

作者: 01_小小鱼_01 | 来源:发表于2018-06-25 00:38 被阅读4次

Android系统中的Service一般是在后台长时间运行的,有时候我们需要一种轻量级的Service在后台运行结束之后就主动停止的,这时候推荐使用IntentService,可以看做是Service和HandlerThread的结合体,在完成了使命之后会自动停止,适合需要在工作线程处理UI无关任务的场景。

其具有如下几个特点:

  • IntentService 是继承自 Service 并处理异步请求的一个类,在 IntentService 内有一个工作线程来处理耗时操作。
  • 当任务执行完后,IntentService 会自动停止,不需要我们去手动结束。
  • 如果启动 IntentService 多次,那么每一个耗时操作会以工作队列的方式在 IntentService 的 onHandleIntent 回调方法中执行,依次去执行,使用串行的方式,执行完自动结束。
// IntentService继承自Service,内部有一个HandlerThread对象。
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.
     */
    public IntentService(String name) {
        super();
        mName = name;
    }

    /**
     * Sets intent redelivery preferences.  Usually called from the constructor
     */
    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(@Nullable 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
    public int onStartCommand(@Nullable 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
     */
    @Override
    @Nullable
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     */
    @WorkerThread
    protected abstract void onHandleIntent(@Nullable Intent intent);
}

相关文章

  • 组件之IntentService源码解析

    一、IntentService源码解析 (1)IntentService源码 每一个时刻只能处理一个intent请...

  • IntentService 源码解析

    Android系统中的Service一般是在后台长时间运行的,有时候我们需要一种轻量级的Service在后台运行结...

  • IntentService源码解析

    由于IntentService的源码涉及到了Handler、Looper、MessageQueue等知识点,所以如...

  • IntentService源码解析

    上一篇我们分析了Android中的线程间通信HandlerThread的原理.HandlerThread充分的利用...

  • IntentService源码解析

    前几篇文章带领大家对HandlerThread的用法及原理进行了深入探索,为了巩固大家对HandlerThread...

  • IntentService源码解析

    为什么我们需要IntentService ? Android中的IntentService是继承自Service类...

  • IntentService源码解析

    概述 IntentService是Service的子类,通过实现onHandleIntent(Intent int...

  • IntentService源码解析

    上套路,说废话。IntentService是个Service的管理器,源码也不是很难,帮助我们简化任务创建。当然在...

  • IntentService源码解析

    如果此时你对Service有些模糊可先简单浏览一下Andriod中各种服务 首先看一下Google给的介绍 作为服...

  • IntentService源码阅读

    IntentService源码阅读 作者:jianzi原文链接:IntentService源码阅读更新时间:201...

网友评论

      本文标题:IntentService 源码解析

      本文链接:https://www.haomeiwen.com/subject/vwheyftx.html