版权声明:本文出自汪磊的博客,转载请务必注明出处。
一、IntentService概述及使用举例
IntentService内部实现机制用到了HandlerThread,如果对HandlerThread不了解的话建议先看上篇文章:Android HandlerThread使用介绍以及源码解析。
IntentService是神马玩意?它就是一个Service,但是我们知道在Service中不能进行耗时操作,如果要进行耗时操作的话我们就需要自己new一个子线程在里面进行耗时操作,然而这一切谷歌工程师早就替我们做好了,只需使用IntentService即可,自己会启动一个子线程做耗时操作,无需我们自己管理子线程,并且完成任务后会自动停止。这么好的玩意赶紧看下怎么使用。
接下来我们编写一个小Demo,模拟在IntentService中进行网络通信,并且成功后通知主线程,主线程做出相应反应。Demo中通过广播接收者实现子线程与主线程的通信。
首先我们编写IntentService,如下:
public class MyIntentService extends IntentService { public static final String TAG = "WL"; public MyIntentService() {
//为子线程起一个名字
super("IntentService"); } //子线程中执行
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
if(null !=intent){
String action = intent.getAction();
if(action.equalsIgnoreCase(MainActivity.ACTION_START)){ String url = intent.getStringExtra(MainActivity.URL);
Log.i(TAG, "开始请求_URL:"+url);
//模拟网络请求
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(TAG, "请求成功");
//
handlerRequestSuccess();
}
}
} //
private void handlerRequestSuccess() {
// 发送广播通知主线程请求成功
Intent intent = new Intent(MainActivity.ACTION_START);
intent.putExtra("msg", "请求成功");
sendBroadcast(intent);
} }
主要逻辑就是继承IntentService,重写onHandleIntent方法,onHandleIntent运行在子线程可以进行耗时操作,在模拟网络请求成功的时候发送广播。
接下来看下MainActivity逻辑:
public class MainActivity extends Activity { //
public static final String ACTION_START= "START_HTTP_REQUEST";
public static final String URL= "URL";
private MyBroadcastReceiver myBroadcastReceiver; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册广播接收者
myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_START);
registerReceiver(myBroadcastReceiver, filter);
} @Override
protected void onResume() {
//
super.onResume();
Intent intent = new Intent(this, MyIntentService.class);
intent.setAction(ACTION_START);
intent.putExtra(URL, "http://www.cnblogs.com/leipDao/");
startService(intent);//必须是start方式启动服务,bind方法无效,分析源码的时候就明白了
} @Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(myBroadcastReceiver);
} private class MyBroadcastReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) { String action = intent.getAction();
if(action.equalsIgnoreCase(ACTION_START)){ String extra = intent.getStringExtra("msg");
Log.i(MyIntentService.TAG, "onReceive::::"+extra);
}
}
}
}
逻辑也是比较简单,onCreate的时候注册广播接收者,用于接收IntentService中模拟网络请求成功的时候发送的广播,onResume的时候启动服务。onDestroy的时候移除广播接收者。Demo比较简单,运行程序打印如下:
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) {
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
} /**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public IntentService(String name) {
super();
mName = name;
} 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(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(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
} @Override
public void onDestroy() {
mServiceLooper.quit();
} @Override
public IBinder onBind(Intent intent) {
return null;
} @WorkerThread
protected abstract void onHandleIntent(Intent intent);
}
很简短吧,出去注释不到100行,接下来我们分析重点部分。
24-27行,构造函数传递进来一个name,用于第40行初始化HandlerThread,就是子线程的名字。
接下来看34-45行onCreate()的时候:主要就是初始化一个HandlerThread以及ServiceHandler,7-17行发现ServiceHandler就是一个普通的Handler而已。看过上一篇Android HandlerThread使用介绍以及源码解析应该明白,ServiceHandler运行在子线程中,这里就不仔细解释了,不明白的地方可以查看Android HandlerThread使用介绍以及源码解析。
73-75行发现onBind的时候什么也没做,就是返回null,还记得demo中注释吗必须以start方式启动服务,因为bing方式启动什么也没做。
48-53行onStart的时候发送一个消息到ServiceHandler,第14行handleMessage的时候直接调用onHandleIntent方法,紧接着15行结束本次任务。
到这里主要流程就完了,其本质就是利用了HandlerThread保证ServiceHandler在子线程运行机制,在我们以start方式启动服务的时候,执行到service生命周期方法onStart的时候就会发送一个消息,然后在ServiceHandler中handleMessage方法执行onHandleIntent方法,子类重写此方法实现耗时任务即可。
好了,到此IntentService就分析完了,理解IntentService最核心的就是先理解HandlerThread。
本篇到此为止,希望对你有用。