为什么要提供 IntentService
解析 Service 是执行在UI线程的,不能进行耗时操作。
IntentService 为什么可以执行耗时操作
- ServiceHandler 是 Handler 的一个子类
- HandlerThread 是 Thread 的一个子类
- 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开发艺术探索》