`
hyshucom
  • 浏览: 808279 次
文章分类
社区版块
存档分类
最新评论

Service详解(一)

 
阅读更多

类概述


服务是一个应用程序组件,代表一个应用程序的desire执行,而不是与用户交互的长时间运行的操作或提供其他应用程序使用的功能。每个服务类必须有一个相应<service>AndroidManifest.xml 申明。服务就可以开始通过Context.startService()Context.bindService()

请注意,服务,像其他的应用程序对象,在其宿主进程的主线程运行。这意味着,如果你的服务会做任何CPU密集型(如MP3播放)或阻塞(如网络)的操作,它应该生成它自己的线程在做这项工作。更多的信息,请参照进程和线程IntentService类 ​​是可作为服务标准的实施,有其自己的线程,where it schedules its work to be done。

这里所涉及的主题:

  1. 什么是服务?
  2. 服务生命周期
  3. 权限
  4. 流程的生命周期
  5. Local Service Sample

  6. 远程信使服务样本

开发指南

为详细讨论有关如何创建服务,阅读服务开发指南。

什么是服务?

Most confusionabout the Service class actually revolves around what it isnot:(服务不是什么)

  • 一个服务是不是一个单独的进程。服务对象本身并不意味着它是在其自身的进程运行,除非另有规定,在同一进程中运行的应用的一部分。
  • 一个服务是不是一个线程。这是不是意味着自己做的工作的主线程(以避免应用程序不响应错误)。

因此,服务本身其实很简单,提供了两个主要特点:

  • 一个应用程序告诉系统设施有关的东西,它希望在这样的背景(即使用户没有直接与应用程序交互)。这相当于电话Context.startService(),这要求系统调度运行服务工作,直到明确停止服务。
  • 设施揭露其功能的一些其他应用程序的应用程序。这相当于电话Context.bindService(),这使得要作出一个长期的连接服务,以便与它交互。

实际创建服务组件时,这两种原因,所有该系统实际上并实例化组件并调用它的onCreate()和在主线程上的任何其他适当的回调。它是实施适当的行为,如创建一个辅助线程在它的工作,这些服务。

请注意,因为服务本身就是这么简单,你可以使简单或复杂的相互作用,如你想:你从它当作一个本地Java对象进行直接的方法调用(按说明本地服务样品),提供一个完整的remoteable接口使用的AIDL。

服务生命周期

有两个原因可以通过系统的运行服务。如果有人呼吁Context.startService()然后系统将检索服务(创建和调用它的onCreate()方法,如果需要的话),然后调用其onStartCommand(Intent, int, int)由客户提供的参数的方法。直到Context.stopService()stopSelf()被称为运行的服务将继续在这一点上。注意多个到Context.startService()的调用不能嵌套(尽管他们在多个相应的调用结果onStartCommand()),所以没有多少次,它启动了一个服务的问题,将被停止,一旦Context.stopService()或stopSelf ()被调用,但是,服务可以使用其stopSelf(int)的方法,以确保服务不会停止,直到开始的意图已处理。

对于启动的服务,还有另外两个主要的操作模式,他们可以决定运行,取决于他们从onStartCommand()返回的值是被明确启动和停止所需的服务使用START_STICKY,START_NOT_STICKYSTART_REDELIVER_INTENT用于服务,只应保持运行,同时处理发送给他们的任何命令。看到更多细节上的语义链接文件。

客户也可以使用Context.bindService()获得一个持久的连接服务。这同样创建的服务,如果它尚未运行(调用OnCreate()中,而这样做),但不叫onStartCommand()。该服务返回其onBind(Intent)方法,允许客户端,然后拨打电话服务,客户端将收到的IBinder对象。该服务将继续运行,只要建立连接(而不是客户端是否保留了对服务的IBinder参考)。通常IBinder返回是一个复杂的接口已在AIDL书面

服务可以启动,并已连接绑定到它。在这种情况下,系统将继续服务,只要运行它要么开始有一个或多个连接与Context.BIND_AUTO_CREATE标志。一旦这些情况下,既不举行,服务的OnDestroy()方法被调用,有效终止服务。(停止线程,注销接收机)全部清理应该是完整的OnDestroy()后返回。



Permissions

Global access to a service can be enforced when it is declared in its manifest's<service>tag. By doing so, other applications will need to declare a corresponding<uses-permission>element in their own manifest to be able to start, stop, or bind to the service.

As ofGINGERBREAD, when usingContext.startService(Intent), you can also setIntent.FLAG_GRANT_READ_URI_PERMISSIONand/orIntent.FLAG_GRANT_WRITE_URI_PERMISSIONon the Intent. This will grant the Service temporary access to the specific URIs in the Intent. Access will remain until the Service has calledstopSelf(int)for that start command or a later one, or until the Service has been completely stopped. This works for granting access to the other apps that have not requested the permission protecting the Service, or even when the Service is not exported at all.

In addition, a service can protect individual IPC calls into it with permissions, by calling thecheckCallingPermission(String)method before executing the implementation of that call.

See theSecurity and Permissionsdocument for more information on permissions and security in general.

Process Lifecycle

The Android system will attempt to keep the process hosting a service around as long as the service has been started or has clients bound to it. When running low on memory and needing to kill existing processes, the priority of a process hosting the service will be the higher of the following possibilities:

  • If the service is currently executing code in itsonCreate(),onStartCommand(), oronDestroy()methods, then the hosting process will be a foreground process to ensure this code can execute without being killed.

  • If the service has been started, then its hosting process is considered to be less important than any processes that are currently visible to the user on-screen, but more important than any process not visible. Because only a few processes are generally visible to the user, this means that the service should not be killed except in extreme low memory conditions.

  • If there are clients bound to the service, then the service's hosting process is never less important than the most important client. That is, if one of its clients is visible to the user, then the service itself is considered to be visible.

  • A started service can use thestartForeground(int, Notification)API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

Note this means that most of the time your service is running, it may be killed by the system if it is under heavy memory pressure. If this happens, the system will later try to restart the service. An important consequence of this is that if you implementonStartCommand()to schedule work to be done asynchronously or in another thread, then you may want to useSTART_FLAG_REDELIVERYto have the system re-deliver an Intent for you so that it does not get lost if your service is killed while processing it.

Other application components running in the same process as the service (such as anActivity) can, of course, increase the importance of the overall process beyond just the importance of the service itself.


参考:http://wikidroid.sinaapp.com/Service

http://developer.51cto.com/art/201002/183554.htm

http://www.cnblogs.com/charley_yang/archive/2011/01/09/1931043.html

Android中Service(服务)详解


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics