android developer tiny share-20161101
来源:互联网 发布:数据的宽度 编辑:程序博客网 时间:2024/06/05 07:12
今天讲IntentService,它是android实现的Service + Thread的一个轻量级框架,你可以使用它实现创建一个队列式的多个线程任务,在Service中运行,运行完成后,Service会自动停止。
下面是android developer官方文档的讲解:
扩展 IntentService 类
由于大多数启动服务都不必同时处理多个请求(实际上,这种多线程情况可能很危险),因此使用 IntentService 类实现服务也许是最好的选择。
IntentService 执行以下操作:
- 创建默认的工作线程,用于在应用的主线程外执行传递给 onStartCommand() 的所有 Intent。
- 创建工作队列,用于将 Intent 逐一传递给 onHandleIntent() 实现,这样您就永远不必担心多线程问题。
- 在处理完所有启动请求后停止服务,因此您永远不必调用 stopSelf()。
- 提供 onBind() 的默认实现(返回 null)。
- 提供 onStartCommand() 的默认实现,可将 Intent 依次发送到工作队列和 onHandleIntent() 实现。
以下是 IntentService 的实现示例:
public class HelloIntentService extends IntentService { /** * A constructor is required, and must call the super IntentService(String) * constructor with a name for the worker thread. */ public HelloIntentService() { super("HelloIntentService"); } /** * The IntentService calls this method from the default worker thread with * the intent that started the service. When this method returns, IntentService * stops the service, as appropriate. */ @Override protected void onHandleIntent(Intent intent) { // Normally we would do some work here, like download a file. // For our sample, we just sleep for 5 seconds. try { Thread.sleep(5000); } catch (InterruptedException e) { // Restore interrupt status. Thread.currentThread().interrupt(); } }}
您只需要一个构造函数和一个 onHandleIntent() 实现即可。
如果您决定还重写其他回调方法(如 onCreate()、onStartCommand() 或 onDestroy()),请确保调用超类实现,以便 IntentService 能够妥善处理工作线程的生命周期。
例如,onStartCommand() 必须返回默认实现(即,如何将 Intent 传递给 onHandleIntent()):
@Overridepublic int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); return super.onStartCommand(intent,flags,startId);}
除 onHandleIntent() 之外,您无需从中调用超类的唯一方法就是 onBind()(仅当服务允许绑定时,才需要实现该方法)。
在下一部分中,您将了解如何在扩展 Service 基类时实现同类服务。该基类包含更多代码,但如需同时处理多个启动请求,则更适合使用该基类。
0 0
- android developer tiny share-20161101
- android developer tiny share-20160627
- android developer tiny share-20160628
- android developer tiny share-20160629
- android developer tiny share-20160630
- android developer tiny share-20160701
- android developer tiny share-20160704
- android developer tiny share-20160705
- android developer tiny share-20160706
- android developer tiny share-20160707
- android developer tiny share-20160708
- android developer tiny share-20160711
- android developer tiny share-20160712
- android developer tiny share-20160713
- android developer tiny share-20160714
- android developer tiny share-20160715
- android developer tiny share-20160718
- android developer tiny share-20160719
- kali无法正常上网的解决过程
- su命令--Linux命令应用大词典729个命令解读
- 序列化表单出现乱码--解决方法
- HDU 1058 Humble Numbers 【暴力(DFS)】
- 投入OJ的怀抱~~~
- android developer tiny share-20161101
- 【原创】枚举枚上天,暴力出奇迹(韩信点兵、分钱游戏、硬币面值组合)
- 第十一周—C语言 oj上机题目(趣味小数)
- 14.8.3 Physical Row Structure of InnoDB Tables InnoDB 表的物理行结构
- 关于php从数据库导出到excel中
- ping命令--Linux命令应用大词典729个命令解读
- js实现全选(全部取消)反向选择
- CSS3 Media Queries 与Responsive 设计
- Arraylist中的modCount 的作用