Android 之Service与IntentService的比较

来源:互联网 发布:深圳淘宝店铺装修 编辑:程序博客网 时间:2024/06/16 16:08



转自:http://blog.csdn.net/zhf198909/article/details/6906786

Android之Service与IntentService的比较
分类: Android 27921人阅读 评论(38) 收藏 举报
serviceandroidapplicationthreadasynchronousclass

       不知道大家有没有和我一样,以前做项目或者练习的时候一直都是用Service来处理后台耗时操作,却很少注意到还有个IntentService,前段时间准备面试的时候看到了一篇关于IntentService的解释,发现了它相对于Service来说有很多更加方便之处,今天在这里稍微来总结下我的心得。

    首先IntentService是继承自Service的,那我们先看看Service的官方介绍,这里列出两点比较重要的地方:

      1.A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.

      2.A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).

      稍微翻一下(英文水平一般大笑)

      1.Service不是一个单独的进程 ,它和应用程序在同一个进程中。

      2.Service不是一个线程,所以我们应该避免在Service里面进行耗时的操作

关于第二点我想说下,不知道很多网上的文章都把耗时的操作直接放在Service的onStart方法中,而且没有强调这样会出现Application Not Responding!希望我的文章能帮大家认清这个误区(Service不是一个线程,不能直接处理耗时的操作)。

       有人肯定会问,那么为什么我不直接用Thread而要用Service呢?关于这个,大家可以网上搜搜,这里不过多解释。有一点需要强调,如果有耗时操作在Service里,就必须开启一个单独的线程来处理!!!这点一定要铭记在心。  

       IntentService相对于Service来说,有几个非常有用的优点,首先我们看看官方文档的说明:

         IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests throughstartService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

         This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

         All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.

            稍微翻译理一理,这里主要是说IntentService使用队列的方式将请求的Intent加入队列,然后开启一个worker thread(线程)来处理队列中的Intent,对于异步的startService请求,IntentService会处理完成一个之后再处理第二个,每一个请求都会在一个单独的worker thread中处理,不会阻塞应用程序的主线程,这里就给我们提供了一个思路,如果有耗时的操作与其在Service里面开启新线程还不如使用IntentService来处理耗时操作。下面给一个小例子:

       1.Service:

[java] view plaincopyprint?
  1. package com.zhf.service;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.IBinder;  
  6.   
  7. public class MyService extends Service {  
  8.   
  9.     @Override  
  10.     public void onCreate() {  
  11.         super.onCreate();  
  12.     }  
  13.       
  14.     @Override  
  15.     public void onStart(Intent intent, int startId) {  
  16.         super.onStart(intent, startId);  
  17.         //经测试,Service里面是不能进行耗时的操作的,必须要手动开启一个工作线程来处理耗时操作  
  18.         System.out.println("onStart");  
  19.         try {  
  20.             Thread.sleep(20000);  
  21.         } catch (InterruptedException e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.         System.out.println("睡眠结束");  
  25.     }  
  26.       
  27.     @Override  
  28.     public IBinder onBind(Intent intent) {  
  29.         return null;  
  30.     }  
  31. }  
           2.IntentService:

[java] view plaincopyprint?
  1. package com.zhf.service;  
  2.   
  3. import android.app.IntentService;  
  4. import android.content.Intent;  
  5.   
  6. public class MyIntentService extends IntentService {  
  7.   
  8.     public MyIntentService() {  
  9.         super("yyyyyyyyyyy");  
  10.     }  
  11.   
  12.     @Override  
  13.     protected void onHandleIntent(Intent intent) {  
  14.         // 经测试,IntentService里面是可以进行耗时的操作的  
  15.         //IntentService使用队列的方式将请求的Intent加入队列,然后开启一个worker thread(线程)来处理队列中的Intent  
  16.         //对于异步的startService请求,IntentService会处理完成一个之后再处理第二个  
  17.         System.out.println("onStart");  
  18.         try {  
  19.             Thread.sleep(20000);  
  20.         } catch (InterruptedException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.         System.out.println("睡眠结束");  
  24.     }  
  25. }  

测试主程序:

[java] view plaincopyprint?
  1. package com.zhf.service;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6.   
  7. public class ServiceDemoActivity extends Activity {  
  8.     /** Called when the activity is first created. */  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.         startService(new Intent(this,MyService.class));//主界面阻塞,最终会出现Application not responding  
  14.         //连续两次启动IntentService,会发现应用程序不会阻塞,而且最重的是第二次的请求会再第一个请求结束之后运行(这个证实了IntentService采用单独的线程每次只从队列中拿出一个请求进行处理)  
  15.         startService(new Intent(this,MyIntentService.class));  
  16.         startService(new Intent(this,MyIntentService.class));  
  17.     }  
  18. }  



       


更多4
主题推荐
android应用程序asynchronous测试异步
博文推荐
一份典型的lazarus 编译目标平台为...
OOP的优势
android之BroadcastRec...
Android ANR错误及预防
浅析Android线程模型
解决办法Incorrect line e...
无工具的情况下测试APP性能
如何反编辑Android apk文件
查看评论
32楼 yds854163 昨天 18:08发表 [回复]
谢楼主~~~~~~ 赞一个!!
31楼 sinomike 2014-03-10 17:10发表 [回复]
简单明了。。赞~\(≧▽≦)/~
30楼 hety163 2014-02-20 10:41发表 [回复]
Service不是一个线程?应该不是一个子线程吧?既不是线程又不是进程那是什么……
29楼 jasoncol_521 2014-01-22 14:07发表 [回复]
写的非常好!容易理解!
28楼 ghl10979 2014-01-14 13:55发表 [回复]
很有帮助,非常感谢!
27楼 zqxiaoqiao 2013-12-19 14:56发表 [回复]
说的很清楚,感谢楼主!
26楼 chshoo 2013-12-12 09:52发表 [回复]
非常清晰!支持一下!
25楼 happyness_xie 2013-11-26 15:08发表 [回复]
get
24楼 cml_bright 2013-11-13 22:08发表 [回复]
简单明了,不错,明白了
23楼 Android-Linux 2013-11-12 10:25发表 [回复]
xuexila
22楼 lyfadd85 2013-10-28 09:33发表 [回复]
哟西,清晰明了
Re: lyfadd85 2013-10-28 09:38发表 [回复]
回复lyfadd85:不知道有没有提供取消当前队列中未处理的消息,进而处理最新的请求的接口,
21楼 裂风 2013-08-13 08:56发表 [回复]
表示赞赏。,学习了
20楼 tzguo1314 2013-07-02 19:27发表 [回复]
写错了, All requests are handled on a single worker thread。
所有的请求都交给唯一的一个工作线程处理。而不是“每一个请求都会在一个单独的worker thread中处理”
19楼 yzyeilin 2013-06-24 17:55发表 [回复]
intentservice的缺点是什么
18楼 bt131476 2013-05-03 16:55发表 [回复]
简单明了,谢谢分享!
17楼 rabbitinhere 2013-05-02 20:57发表 [回复]
感谢分享~~学习了
16楼 Avin 2013-04-12 15:04发表 [回复]
谢谢楼主,理解深刻!用一下就更深刻了!
15楼 司马奔 2013-04-01 14:43发表 [回复]
楼主写的简洁明了,很容易就理解这个知识点了。谢谢楼主。收藏了
14楼 ll397879213 2013-03-21 10:33发表 [回复]
不错!!
13楼 任付江 2013-01-30 21:31发表 [回复]
intentService 的优势是什么呢?安全,异步避免anr
Re: qeqeqe236 2013-04-28 16:59发表 [回复]
回复renfujiang: 不用自己处理线程和队列。。
Re: 人道渺渺 2013-09-14 20:47发表 [回复]
回复qeqeqe236:可是楼猪,我在service中可以 开很多线程并发处理,和不会阻塞main线程,不排队效率高。你要是一定喜欢排队也可以自己写个 线程内嵌handler去实现啊。handler就是消息队列机制,这个intentService我咋看咋别扭了,楼猪~~楼主我打了错别字
Re: seuduck 2013-10-09 17:56发表 [回复]
回复joychine:intentservice的实现其实就是service里面起了一个handlerThread,thread的handlemessage调用onHandleIntent函数,所以本质上没什么不一样,有兴趣可以看下源码
12楼 点背 2013-01-24 18:26发表 [回复]
收藏了
11楼 鲁面羊 2012-12-16 12:45发表 [回复]
入门好资料,谢谢楼主
10楼 逍遥K杰 2012-11-23 09:58发表 [回复]
IntentService实现了一种排队机制,前一个任务完了之后才能开始下一个任务。而在Service里直接开线程可以同时执行啊,IntentService有何优势呢?
Re: wangshiming 2013-10-29 10:10发表 [回复]
回复keshuangjie:android service 里面是不能开启过多的线程的
9楼 瑞雪漫天 2012-11-21 17:03发表 [回复]
向你学习,致敬
8楼 jorig 2012-11-17 11:38发表 [回复]
非常感谢。
7楼 zmttpassion 2012-11-09 10:05发表 [回复]
多谢亲! 讲的很清楚!受教了!
希望您继续写下去让更多人受益!
6楼 ItNoob 2012-11-09 10:04发表 [回复]
顶楼主
5楼 赤子Anatta 2012-09-11 15:25发表 [回复]
学习了~ 谢谢~
4楼 huayanlong 2012-08-10 14:59发表 [回复]
谢谢楼主,收了……
3楼 神流浪 2012-07-27 16:44发表 [回复]
谢谢楼主
2楼 zozing 2012-05-21 15:09发表 [回复]
分析很到位
1楼 cmoaciopm 2012-04-24 14:10发表 [回复] [引用] [举报]
写的很清晰,多谢楼主!

0 0
原创粉丝点击