IntentService
来源:互联网 发布:mac怎么下载qq游戏 编辑:程序博客网 时间:2024/06/11 17:04
IntentService是Service的子类,它增加了额外的功能。
-Service 本身存在的问题,它自身不能开启线程,Service所有应用都运行在主线程。
因此不能在service中执行耗时动作,因此,如果要处理耗时任务,则需要
- 开启另外一条线程来处理
- 疑虑:既然服务自身不能开启线程,那么和在其他地方开启线程有什么区别那?
- 原因:
- 因为Activity可能被用户退出,BroadcastReceiver的生命周期也很短。由此,
可能出现的情况是,子程序还没有运行结束的情况下,Activity 已经退出,
-或者 BroadcastReceiver 也结束了,此时它们所在的进程就成了空进程,系统在需要资源的时候会优先该进程,宿主进程结束了,
- 该进程中的所有子线程也会被终止。任务无法完成。
public class MyIntentService extends IntentService { /** * Creates an IntentService. Invoked by your subclass's constructor. * * @param name Used to name the worker thread, important only for debugging. */ /* * Service不会专门启动一条线程,Service与所在的应用在一个线程之中,因此不可以在Service中直接执行耗时任务。 * Android专门提供了一个IntentService类,这个类就很好的解决了忘记在服务中开启线程,任务结束后关闭线程。 * */ public MyIntentService(String name) { super(name); } @Override protected void onHandleIntent(Intent intent) { //这个方法是在子线程中运行 //IntentService会创建单独的worker线程来处理所有的intent请求,开发者无需处理多线程问题 //当所有请求结束后,服务会自动停止 //为onBinder方法提供了默认实现,默认返回空 //为onStartCommand方法提供了默认实现,该实现会将Intent添加到队列 }
0 0
- IntentService
- IntentService
- intentservice
- IntentService
- IntentService
- IntentService
- IntentService
- IntentService
- IntentService
- IntentService
- IntentService
- IntentService
- IntentService
- IntentService
- IntentService
- IntentService
- IntentService
- intentservice
- struct、union
- 《疯狂Java讲义(第3版)》.(李刚)——前言
- 对于酷派手机无法打印log问题解决方法
- Timus 1820. Ural Steaks
- java从本地向另外一个地址发送请求
- IntentService
- 杭电5645
- linux驱动中实现上层select接口
- HDU - 5665 Lucky (技巧)
- 写博客的初衷
- validator验证指定字符
- 精确毫秒
- B-树与B+树
- mssql数据库,数据库同步,分布式数据库,数据库集群,如何实现