Android多线程(2)IntentService学习

IntentService

一、IntentService概述

IntentService的特点:

它本质是一种特殊的Service,继承自Service并且本身就是一个抽象类
它可以用于在后台执行耗时的异步任务,当任务完成后会自动停止
它拥有较高的优先级,不易被系统杀死(继承自Service的缘故),因此比较适合执行一些高优先级的异步任务
它内部通过HandlerThread和Handler实现异步操作
创建IntentService时,只需实现onHandleIntent和构造方法,onHandleIntent为异步方法,可以执行耗时操作

使用:通过查看源码我们发现:

public abstract class IntentService extends Service

IntentService继承了Service,说明它的本质就是一个普通的Service,然后,在普通的Service上加入一些功能,既然是一个Service,我们来看他重写的生命周期

onCreate

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

这里我们发现,它创建了一个HandlerThread类(HandlerThread详解
ServiceHandler类源码:

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

这个类就是自定义的一个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;
    }

修改了,Service被销毁的后的策略,我们继续

onStart(intent, startId);

在onStartCommand中调用的onStart方法:

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

哈哈,这里就是IntentService的精髓了,使用mServiceHandler发送一个msg,我们来看看,这个ServiceHandler

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

首先注意,这个handler是在刚才HandlerThread开启的新线程中,
onHandleIntent((Intent)msg.obj);先调用这个方法,然后就直接把service关掉了。

其实我们看源码发现关键是这个onHandleIntent方法,

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

原来是抽象方法,必须子类实现,那么说明这个方法会在新线程中调用,然后调用完成直接就把service关闭了。

至此,我们知道了如何去用IntentService了吧,我们把我们的代码写在onHandleIntent中,当运行完毕就会回自动关闭service。

费了这么大劲为啥呢?为啥不直接使用thread后者其他的线程方式不就好了,

我们来看IntentService类的官方解释找找答案:
1、我认为我们使用Service组件可以承载我们Thread使用的资源,同时可以很好的管理线程,防止造成资源回收后,无法管理线程,或者造成内存泄漏。

总结:方便thread的管理,让thread跟service生命周期绑定。

个人理解难免不到位,还望大神指教,
我的邮箱:[email protected]

猜你喜欢

转载自blog.csdn.net/liujian8654562/article/details/80480489