码迷,mamicode.com
首页 > 移动开发 > 详细

Android中Service与IntentService的使用比较

时间:2016-02-27 23:34:20      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

不知道大家有没有和我一样,以前做项目或者 练习的时候一直都是用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里,就必须开启一个单独的线程来处理,这点一定要铭记在心。

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

  IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(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.

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

  由于服务进程的优先级高于后台进程, 因此如果 activity需要执行耗时操作, 最好还是启动一个service来完成. 当然, 在activity中启动子线程完成耗时操作也可以,但是这样做的缺点在于,一旦activity不再可见,activity所在的进程成为后台进程, 而内存不足时后台进程随时都有可能被系统杀死(但是启动service完成耗时操作会带来数据交互的问题, 比如耗时操作需要实时更新UI控件的状态的话,service就不是一个好的选择)。 基于同样的考虑, 在BroadcastReceiver中也不应该执行耗时操作, 而应该启动service来完成(当然, BroadcastReceiver的生命周期过于短暂, 也决定了不能在其中执行耗时操作)。

  总结:IntentService是一个通过Context.startService(Intent)启动可以处理异步请求的 Service,使用时你只需要继承IntentService和重写其中的onHandleIntent(Intent)方法接收一个Intent对 象,在适当的时候会停止自己(一般在工作完成的时候)。所有的请求的处理都在一个工作线程中完成,它们会交替执行(但不会阻塞主线程的执行),一次只能执行一个请求。

    这是一个基于消息的服务,每次启动该服务并不是马上处理你的工作,而是首先会创建对应的Looper,Handler并且在MessageQueue中添 加的附带客户Intent的Message对象,当Looper发现有Message的时候接着得到Intent对象通过在 onHandleIntent((Intent)msg.obj)中调用你的处理程序,处理完后即会停止自己的服务,意思是Intent的生命周期跟你的 处理的任务是一致的,所以这个类用下载任务中非常好,下载任务结束后服务自身就会结束退出。

 总结IntentService的特征有:

(1)会创建独立的worker线程来处理所有的Intent请求;

(2)会创建独立的worker线程来处理onHandleIntent()方法实现的代码,无需处理多线程问题;

(3)所有请求处理完成后,IntentService会自动停止,无需调用stopSelf()方法停止Service

Android中Service与IntentService的使用比较

标签:

原文地址:http://www.cnblogs.com/wxishang1991/p/5223817.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!