IntentService详解
2018-07-10 本文已影响0人
menteelin
一 IntentService概述
- 本质是一个Service,继承自Service且是个抽象类
- 它用来在后台执行耗时的异步任务,当任务执行完毕后自动停止
- 它内部通过HandlerThread和Handler来实现异步操作
- 用户通过实现onHandleIntent方法,并在此方法中处理耗时任务
二 IntentService使用
- 直接上代码
public class MyIntentService extends IntentService {
private static final String ACTION_FOO = "com.jrmf360.service.action.FOO";
private static final String ACTION_BAZ = "com.jrmf360.service.action.BAZ";
private static final String EXTRA_PARAM = "com.jrmf360.service.extra.PARAM";
public MyIntentService() {
super("MyIntentService");
}
/**
* Starts this service to perform action Foo with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
public static void startActionFoo(Context context, String param) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_FOO);
intent.putExtra(EXTRA_PARAM, param);
context.startService(intent);
}
/**
* Starts this service to perform action Baz with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
public static void startActionBaz(Context context, String param) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_BAZ);
intent.putExtra(EXTRA_PARAM, param);
context.startService(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
Log.e(getClass().getSimpleName(),"当前线程:"+Thread.currentThread().getName());
String action = intent.getAction();
String param = intent.getStringExtra(EXTRA_PARAM);
if (ACTION_FOO.equals(action)) {
handleActionFoo(param);
} else if (ACTION_BAZ.equals(action)) {
handleActionBaz(param);
}
Log.e(getClass().getSimpleName(),param+"处理结束");
}
}
/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionFoo(String param1) {
Log.e(getClass().getSimpleName(),"当前处理任务:"+param1);
}
/**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
private void handleActionBaz(String param1) {
Log.e(getClass().getSimpleName(),"当前处理任务:"+param1);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(getClass().getSimpleName(),"IntentServer 销毁");
}
}
我们创建一个MyIntentService继承自IntentService,然后实现onHandleIntent方法,在该方法中获得intent传递过来的参数并处理耗时任务。
- MyIntentService使用
在Activity中直接调用
MyIntentService.startActionBaz(this,"吃饭");
MyIntentService.startActionFoo(MainActivity.this,"打游戏");
- 打印日志
07-10 16:24:43.652 27125-27178/com.jrmf360.service E/MyIntentService: 当前线程:IntentService[MyIntentService]
07-10 16:24:43.652 27125-27178/com.jrmf360.service E/MyIntentService: 当前处理任务:吃饭
07-10 16:24:43.652 27125-27178/com.jrmf360.service E/MyIntentService: 吃饭处理结束
07-10 16:24:43.660 27125-27178/com.jrmf360.service E/MyIntentService: 当前线程:IntentService[MyIntentService]
07-10 16:24:43.660 27125-27178/com.jrmf360.service E/MyIntentService: 当前处理任务:打游戏
07-10 16:24:43.660 27125-27178/com.jrmf360.service E/MyIntentService: 打游戏处理结束
07-10 16:24:43.660 27125-27125/com.jrmf360.service E/MyIntentService: IntentServer 销毁
从日志中我们可以发现,IntentService开启之后会在线程中处理异步任务,线程名字为IntentService[MyIntentService]是我们在构造函数中设置的。并且多个任务是串行执行,当所有的任务处理完毕,销毁该IntentService。
三 IntentService原理分析
- 首先我们看下IntentService的源码
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) {
//处理消息并且在处理完毕后,销毁该IntentService
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
}
/**
*构造方法,并且设置线程的名字
*
*/
public IntentService(String name) {
super();
mName = name;
}
public void setIntentRedelivery(boolean enabled) {
mRedelivery = enabled;
}
@Override
public void onCreate() {
super.onCreate();
//创建HandlerThread 并且开始该线程
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
//使用HanderThread的Looper对象去创建handler
//所以该Handler也在此线程中处理任务
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
//在此方法中利用Handler发送消息,从而可以在Handler中的handleMessage方法中处理该消息
@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;
}
@Override
public void onDestroy() {
mServiceLooper.quit();
}
/**
* Unless you provide binding for your service, you don't need to implement this
* method, because the default implementation returns null.
* @see android.app.Service#onBind
*/
@Override
@Nullable
public IBinder onBind(Intent intent) {
return null;
}
@WorkerThread
protected abstract void onHandleIntent(@Nullable Intent intent);
}
分析源码可知,开启IntentService的时候在onCreate方法中创建了一个HandlerThread,HandlerThread本身是一个线程并且拥有Looper对象,使用这个Looper对象创建Handler就可以在该线程中处理Handler发送的消息。
接着往下看,IntentService在onStartCommand方法中调用了onStart,而在onStart方法中获得了一个Message并使用mServiceHandler发送这个消息。因此我们一旦调用startService方法开启服务,就会走到onHandleIntent方法中并且该方法是在HandlerThread线程中被调用。
再看ServiceHandler中的handleMessage方法,处理完消息后,会调用stopSelf方法停止该服务。这就是任务处理完之后会调用IntentService的onDestroy方法的原因。