IntentService 从源码分析

为什么要提供 IntentService

解析 Service 是执行在UI线程的,不能进行耗时操作。

IntentService 为什么可以执行耗时操作
  1. ServiceHandler 是 Handler 的一个子类
  2. HandlerThread 是 Thread 的一个子类
  3. onHandleIntent 抽象方法需要自己实现
    @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); // 创建一个Handler并且与HandlerThread的Looper进行绑定 
    }

显而易见 内部创建了一个线程并且开启了线程
然后创建一个Handler并与指定的线程的Looper进行绑定

下面看看线程内部执行了什么

    @Override
    public void run() {
        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper(); // 得到当前线程的Looper
            notifyAll();
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop(); // 运行消息队列
        mTid = -1;
    }

一样的简单 里面首先得到当前线程的Looper
然后开始运行消息队列

下面看看Handler的实现

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

是不是觉得so easy。没错就是这么简单。
收到消息以后进行了一下消息的转发,由IntentService的抽象方法去做处理,然后停止。

关于onHandleIntent的定义。只有一个地方需要注意,就是该方法在工作线程里面执行的

    @WorkerThread
    protected abstract void onHandleIntent(@Nullable Intent intent);
如何通过ServiceHandler发送消息

废话不多说直接上代码

    @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 {@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;
    }

在Service的 onStart onStartCommand 里面发送的消息

关于Service的机制请大家参考其他资料
打个call 《Android开发艺术探索》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值