IntentService是继承并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统的Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们手动去控制或stopSelf()。另外,可以启动IntentService多次,而每一个耗时操作会以工作队列的方式在IntentService的onHandleIntent回调方法中执行,并且,每次只会执行一个工作线程,执行完第一个再执行第二个,以此类推。

先来看一下IntentService类的源码:

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

定义一个IntentService的子类:

public class MIntentService extends IntentService {

    public MIntentService(){
super("MIntentService");
} /**
* Creates an IntentService. Invoked by your subclass's constructor.
* @param name Used to name the worker thread, important only for debugging.
*/
public MIntentService(String name) {
super(name);
} @Override
public void onCreate() {
Log.e("MIntentService--", "onCreate");
super.onCreate();
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("MIntentService--", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
} @Override
protected void onHandleIntent(Intent intent) {
Log.e("MIntentService--", Thread.currentThread().getName() + "--" + intent.getStringExtra("info") );
for(int i = 0; i < 100; i++){ //耗时操作
Log.i("onHandleIntent--", i + "--" + Thread.currentThread().getName());
}
} @Override
public void onDestroy() {
Log.e("MIntentService--", "onDestroy");
super.onDestroy();
}
}

开启IntentService服务:

 public void intentClick(View v){
Intent intent = new Intent(this, MIntentService.class);
intent.putExtra("info", "good good study");
startService(intent);
}

点击按钮之后输出结果为(过滤log.e):

10-25 16:54:58.852  27135-27135/com.example.lenovo.myintentservicedemo E/MIntentService--﹕ onCreate
10-25 16:54:58.852 27135-27135/com.example.lenovo.myintentservicedemo E/MIntentService--﹕ onStartCommand
10-25 16:54:58.856 27135-27354/com.example.lenovo.myintentservicedemo E/MIntentService--﹕ IntentService[MIntentService]--good good study
10-25 16:54:58.879 27135-27135/com.example.lenovo.myintentservicedemo E/MIntentService--﹕ onDestroy

  Intent服务开启后,执行完onHandleIntent里面的任务就自动销毁结束,通过打印的线程名称可以发现是新开了一个线程来处理耗时操作的,即是耗时操作也可以被这个线程管理和执行,同时不会产生ANR的情况。

最新文章

  1. MongoDB初识
  2. 用mac的terminal通过公私钥和ssh登录Linux
  3. java EE中使用PO和VO的注意事项
  4. 使用Asyncio的Coroutine来实现一个有限状态机
  5. MySQL Cluster 配置详细介绍
  6. paper 17 : 机器学习算法思想简单梳理
  7. MQTT客户端与服务代理的案列
  8. C++转换unicode utf-8 gb2312编码
  9. ajax和jsonp的封装
  10. php hook 之简单例子
  11. 一个完善的ActiveX Web控件教程
  12. Windows终端工具_MobaXterm
  13. threding模块的其他用法
  14. 微信小程序(一)快递查询
  15. FlinkCEP - Complex event processing for Flink
  16. curl常用用法
  17. centos mysql忘记密码
  18. ubuntu 16.04网速监控脚本
  19. Eclipse下搭建SWT与Swing图形界面开发环境
  20. PHP变量定义及工作原理

热门文章

  1. 图解Git命令
  2. pythion 第二弹
  3. 刨根究底字符编码之十二——UTF-8究竟是怎么编码的
  4. jquery中的$(document).ready()使用小结
  5. echarts仪表盘如何设置图例(legend)
  6. [BZOJ3675]序列分割
  7. Bash中单引号和双引号的区别
  8. 动态读取文件持续显示在UI上
  9. Ext表格分页
  10. python+NLTK 自然语言学习处理二:文本