类概述
服务是一个应用程序组件,代表一个应用程序的desire执行,而不是与用户交互的长时间运行的操作或提供其他应用程序使用的功能。每个服务类必须有一个相应<service>
在AndroidManifest.xml
申明
。服务就可以开始通过Context.startService()
和Context.bindService()
。
请注意,服务,像其他的应用程序对象,在其宿主进程的主线程运行。这意味着,如果你的服务会做任何CPU密集型(如MP3播放)或阻塞(如网络)的操作,它应该生成它自己的线程在做这项工作。更多的信息,请参照进程和线程。IntentService
类
是可作为服务标准的实施,有其自己的线程,where it schedules its work to be done。
这里所涉及的主题:
- 什么是服务?
- 服务生命周期
- 权限
- 流程的生命周期
-
Local Service Sample
- 远程信使服务样本
开发指南
为详细讨论有关如何创建服务,阅读服务开发指南。
什么是服务?
Most confusionabout the Service class actually revolves around what it isnot:(服务不是什么)
- 一个服务是不是一个单独的进程。服务对象本身并不意味着它是在其自身的进程运行,除非另有规定,在同一进程中运行的应用的一部分。
- 一个服务是不是一个线程。这是不是意味着自己做的工作的主线程(以避免应用程序不响应错误)。
因此,服务本身其实很简单,提供了两个主要特点:
实际创建服务组件时,这两种原因,所有该系统实际上并实例化组件并调用它的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_STICKY
或START_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_PERMISSION
and/orIntent.FLAG_GRANT_WRITE_URI_PERMISSION
on
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_REDELIVERY
to
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(服务)详解
分享到:
相关推荐
介绍了普通service和远程AIDL的service,通过代码较详细的介绍了两种service
Web Service架构 Web Service vs. CORBA/DCOM/EJB Web Service是使用Web的新模式 通过程序自动启动和处理商务事务,而并非...能够在一个分布式的计算环境中动态地描述、发布、发现和调用 支持基于Web Service的新型应用
android service使用详解,初步了解service的使用,关注博客 http://himici.com/
服务(Service)是Android 系统中4 个应用程序组件之一(其他的组件详见3.2 节的内容)。服务主要 用于两个目的:后台运行和跨进程访问。通过启动一个服务,可以在不显示界面的前提下在后台运行指定 的任务,这样...
Web Service描述语言 WSDL 详解
Android平台调用为本service详解,android结合为本service应用。
Web Service描述语言 WSDL 详解.pdf
android Service 详解 很详细
本章内容服务(Service)是Android系统中4个应用程序组件之一(其他的组件详见3.2节的内容)。服务主要用于两个目的:后台运行和跨进程访问。通过启动一个服务,可以在不显示界面的前提下在后台运行指定的任务,这样...
七、kubernetes Service详解.pdf
Oracle Service Bus实例开发详解 Oracle Service Bus实例开发详解 Oracle Service Bus实例开发详解
Android Service 详解
Android服务Service_详解文档
xfire开发Web Service接口详解,里面包含图片和文字,适合初学web service的
Web Service主要是为了使原来各孤立的站点之间的信息能够相互通信、共享而提出的一种接口。
文档具体讲解了Android四大组建中重要的一个Service的用法
关于如何学好android之service经典详解pdf,大家可下载参考!
web service开发技术详解 C# extjs
android各组件详解- Service