IntentService的使用
来源:互联网 发布:2017年淘宝客好做吗 编辑:程序博客网 时间:2024/04/29 19:02
使用Service需注意
Service 是在主线程中执行的,所以不能再Service中直接执行网络加载等耗时操作,应该开启子线程进行加载。
使用IntentService
IntentService会自动开起一个工作线程,在此工作线程中执行耗时操作,不会引发ANR异常.
IntentService必须重写onHandleIntent(),并在此方法中执行耗时操作.
IntentService自动重写了onBind()和onStartCommand()方法
IntentService 使用队列来管理请求的Intent,新来的Intent将被加入队列中等待执行,每次IntentService会开启一个新的work线程执行请求 ,该线程只保证同一时刻只处理一个Intent。
继承自IntentService类
public class MyService extends IntentService { public MyService(String name) { super(name); } public MyService() { super(""); } // 此方法会在工作线程中执行,不会阻塞子线程 @Override protected void onHandleIntent(Intent arg0) { try { synchronized (this) { // wait()让当前线程等待10秒 this.wait(10 * 1000); } } catch (InterruptedException e) { e.printStackTrace(); } Log.v("LOG", "finished!!!"); }}
执行此IntentService 时,不会产生异常。
0 0
- IntentService的使用
- Android:IntentService的使用
- Android IntentService 的使用
- 【安卓篇】IntentService的使用
- IntentService的使用
- IntentService 的使用
- Android IntentService的使用
- IntentService的使用介绍
- IntentService的使用
- IntentService的使用
- IntentService的使用
- IntentService的使用
- Android IntentService的使用
- IntentService的使用
- IntentService的原理及使用
- Android中IntentService的使用
- 扩展IntentService类的使用
- IntentService的原理及使用
- Android游戏笔记(六)Android游戏程序框架
- android Imageview Textview纯代码增加按下效果
- 什么是沙盒(sandbox)?沙盒包含哪些文件,描述每个文件的使用场景。如何获取这些文件的路径?如何获取应用程序包中文件的路径?
- 开发流程告诉你,为什么是软件工程师而不是码农
- JUnit中@BeforeClass@AfterClass@Before@After和@Test
- IntentService的使用
- 【T_SQL】 基础 续+++
- 移植speex库
- 如何实现球随鼠标移动
- 复制构造函数 与 赋值函数 的区别
- [疯狂Java]基础类库:System(环境变量、系统属性、系统时间、内存地址、垃圾回收)
- c#初学-多线程中lock用法的经典实例
- 深入浅出Mybatis系列(四)---配置详解之typeAliases别名(mybatis源码篇)
- 房地产为何一枝独秀?