service大家估计比较清楚了,是Android 的四大组件之一,如果你不了解,请百度Android四大组件。
至于什么是IntentService,估计很多人都很陌生,笔者也是,最近在做一个项目,于是接触到了Intentservcie,于是写下来与大家分享下我的经验与收获。
官方给出IntentService的解释是,
An abstract Service
that serializes the handling of the Intents passed upon service start and handles them on a handler thread.
To use this class extend it and implement onHandleIntent(android.content.Intent)
. The Service
will automatically be stopped when the last enqueued Intent
is handled.
由以上的翻译,我们可以大概理解到,IntentService与Service应该有两处不同,一是,IntentService是序列化执行Intent的,也就是,如果向IntentService发送多个Intent,IntentService是不会阻塞的;二是,IntentService执行完Intent后,是会自动关闭的。而Service没有这两个特点。
为了验证以上的两个结论,代码如下,Activity
private Button button1;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
MyIntentService.class);
startService(intent);
}
});
}
Button1启动Service,Button2启动IntentService。
Service代码
public class MyService extends Service {
private String TAG = "MyService";
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i(TAG, "onBind");
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i(TAG, "onCreate");
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i(TAG, "onDestroy");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i(TAG, "onStartCommand");
Log.i(TAG, "Service 线程:" + Thread.currentThread().getId());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}
}
IntentService代码,
public class MyIntentService extends IntentService {
private String TAG = "MyIntentService";
public MyIntentService() {
super("");
}
public MyIntentService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Log.i(TAG, "onHandleIntent");
Log.i(TAG, "IntentService 线程:" + Thread.currentThread().getId());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i(TAG, "onBind");
return super.onBind(intent);
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i(TAG, "onCreate");
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i(TAG, "onDestroy");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
}
运行程序,单击Button1,输出信息如下
单击Buttons,输出信息如下,
首先我们注意到,在Service让线程休眠2s,系统会输出一条语句,The application may be doing too much work on its main thread. 大概意思是,在主线程中,执行了太多的任务。而IntentService系统却没有提示任何信息,所以Service是不太适合执行耗时的后台任务的,如果非要执行的话,可以新建一个线程,然后在新的线程后实现。
其次,我们看下Service与IntentService的生命周期,不同的是,IntentService会自己结束自己,当所有的Intent执行完之后,而Service却不会,只能等待用户手动结束Service。
然后我们,测试下,IntentService的顺序执行Intent,我们快速点击Butoon1两次,输出如下,
点击两次Button2,输出如下,
貌似我们从输出信息中是看不到除了Service不适合执行长时间耗时任务外的任何信息,笔者测试时,也没有弹出ANR(Application Not Responding),但是笔者注意到,两次单击Button1时,Button1的背景色会一直为蓝色(Android系统默认的Button响应单击操作的背景色),这说明,sercive阻塞了主线程,使得在该阶段,系统无法相应用户的操作。至于为什么么有出现ANR,可能是因为不同的Android版本使然吧。笔者的版本为4.1.1。读者可以在其他系统实验下。
OK,以上就是我所有关于IntentService的收获,谢谢大家!
参考网址http://www.cnblogs.com/zhangs1986/p/3602154.html
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1108/532.html