安卓组件之---Service(文档导读)

从类的角度来看,Service是抽象类,public abstract class ,这意味着在继承开发Service子类时,需要实现其抽闲方法abstract method。往文档下面扫了一眼,需要重写的abstract mthod只有一个,是:public abstract IBinder onBind( Intent intent ).暂时不深究啥意思….

回到类的角度,Service直接extends 了ContextWraper类,而ContextWrapper又继承自Context。也就是说,Context类中的方法,Service都可以自由的调用,比如说getResources( )什么的。

那Service有哪些子类呢?看文档有一大堆子类,挑一个出来看:IntentService。IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. 显然,IntentService可以用来处理异步请求。
对于Service的子类,不做过多介绍,有必要再深究。

public abstract class
Service
extends ContextWrapper
implements ComponentCallbacks2
java.lang.Object
android.content.Context
↳ android.content.ContextWrapper
↳ android.app.Service

粗糙的看了一下Service的父类,子类。积累了一点感性认识,下面开始顺着文档翻译。

Class Overview

A Service is an application component representing either an application’s desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding declaration in its package’s AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService(). Service是一个应用组件,一般用于两件事件,一是用以执行一个无须与用户交互的耗时任务,二是为其他应用提供功能(functionality)。每一个service类,必须要在AndroidManifest.xml文件中声明。Service可以被用两种方式启动,用Context.startService( ) ,或者用Context.bindService( )来启动service.

Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done. 这一段主要是讲service这个组件运行在何种进程当中的。并且给建议说,如果你的service要提供的服务是CPU密集型任务,比如说播放MP3或者堵塞操作(连接网络),那么你应该分化出一个自己的线程,并将工作在这个线程线程中完成。

Topics covered here: 文档覆盖的话题有:

 1. What is a Service?啥子是Service
 2. Service Lifecycle Service的生命周期
 3. Permissions      许可制---就是要在AndroidManifest.xml去声明该Service
 4. Process Lifecycle
 5.  Local Service Sample  一个栗子

Developer Guides开发者指南….自己去看洛…..

For a detailed discussion about how to create services, 
read the Services developer guide.

What is a Service? 啥子是Service??

Most confusion about the Service class actually revolves around what it is not:
关于Service的大部分困惑,其实并不是围绕着Service是什么,而是关于Service不是什么:

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.一个Service不是一个独立的进程。Service组件本身并不意味着它能运行于自己的进程。除非其他组件明确规定,Service与其他组件运行在同一个进程当中,好像Service是应用的一部分。
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).
Service不是线程。Service不能脱离主线程工作(避免程序出现该种类型错误:Not Responding errors)。

Thus a Service itself is actually very simple, providing two main
features:由此一个Service就很简单了,它主要具有两个特征:

A facility for the application to tell the system about something it wants to be doing in the background (even when the user is not

directly interacting with the application). This corresponds to calls
to Context.startService(), which ask the system to schedule work
for the service, to be run until the service or someone else
explicitly stop
it.Service可以作为一个在后台运行的组件,无须交互。当Service用于这种用途时,用Context.startService()来启动。
A facility for an application to expose some of its functionality to other applications. This corresponds to calls to
Context.bindService(), which allows a long-standing connection to be made to the service in order to interact with it.
Service作为一个组件,用来响应其他程序的请求,提供某种功能。当Service用于这种用途时,用Context.bindService()来启动。

When a Service component is actually created, for either of these
reasons, all that the system actually does is instantiate the
component and call its onCreate() and any other appropriate callbacks
on the main thread. It is up to the Service to implement these with
the appropriate behavior, such as creating a secondary thread in which
it does its work.

Note that because Service itself is so simple, you can make your
interaction with it as simple or complicated as you want: from
treating it as a local Java object that you make direct method calls
on (as illustrated by Local Service Sample), to providing a full
remoteable interface using AIDL.

**Service Lifecycle**Service的生命周期

There are two reasons that a service can be run by the system. If
someone calls Context.startService() then the system will retrieve the
service (creating it and calling its onCreate() method if needed) and
then call its onStartCommand(Intent, int, int) method with the
arguments supplied by the client. The service will at this point
continue running until Context.stopService() or stopSelf() is called.
Note that multiple calls to Context.startService() do not nest (though
they do result in multiple corresponding calls to onStartCommand()),
so no matter how many times it is started a service will be stopped
once Context.stopService() or stopSelf() is called; however, services
can use their stopSelf(int) method to ensure the service is not
stopped until started intents have been
processed.简单来说就是,如果你想启动一个Service,调用Context.startService(),
系统会调用onCreate(如果有必要),然后再调用onStartCommand(Intent, int,
int)。Service就运行起来了。。。一直运行到方法Context.stopService() 或 stopSelf()被调用。

For started services, there are two additional major modes of
operation they can decide to run in, depending on the value they
return from onStartCommand(): START_STICKY is used for services that
are explicitly started and stopped as needed, while START_NOT_STICKY
or START_REDELIVER_INTENT are used for services that should only
remain running while processing any commands sent to them. See the
linked documentation for more detail on the semantics.

Clients can also use Context.bindService() to obtain a persistent
connection to a service. This likewise creates the service if it is
not already running (calling onCreate() while doing so), but does not
call onStartCommand(). The client will receive the IBinder object that
the service returns from its onBind(Intent) method, allowing the
client to then make calls back to the service. The service will remain
running as long as the connection is established (whether or not the
client retains a reference on the service’s IBinder). Usually the
IBinder returned is for a complex interface that has been written in
aidl.
客户端也可以用方法Context.bindService()来建立一个与Service持续接连的绑定。如果Service还不存在,就需要调用onCreate(),但不像前面一种方式,这里不需要调用onStartCommand()。客户端会接受IBinder对象(IBinder对象是service从它的onBind(Intent)方法中返回的对象),IBinder对象使得客户端能够回调该service。用Context.只要绑定的连接还存在,Service就会一直运行。

A service can be both started and have connections bound to it. In
such a case, the system will keep the service running as long as
either it is started or there are one or more connections to it with
the Context.BIND_AUTO_CREATE flag. Once neither of these situations
hold, the service’s onDestroy() method is called and the service is
effectively terminated. All cleanup (stopping threads, unregistering
receivers) should be complete upon returning from onDestroy().
前面所说的两种方式,可以同时存在与一个service上。既一个service既可以被Context.startService()启动,也可以用方法Context.bindService()绑定。在这种情况,该service的运行时间该如何确定?该如何终结?自己找咯….答案就在眼前…

Local Service Sample 举一个栗子

One of the most common uses of a Service is as a secondary component
running alongside other parts of an application, in the same process
as the rest of the components. All components of an .apk run in the
same process unless explicitly stated otherwise, so this is a typical
situation.Service最常见的用法是将service作为第二个组件运行于程序当中,与其他组件一样,运行在同一个进程当中。

When used in this way, by assuming the components are in the same
process, you can greatly simplify the interaction between them:
clients of the service can simply cast the IBinder they receive from
it to a concrete class published by the
service.当service是这样的用途的时候,既所有组件运行在同一个进程当中。当service是这种用途时,你可以极大的简化service与其他组件之间的交互:service的委托人(client)可以将从service中接受的IBinder对象投射到具体的类上面去。clients
of the service can simply cast the IBinder they receive from it to a
concrete class published by the service,这一句没有太看明白…不要急,等下看看例子就好了。

An example of this use of a Service is shown here. First is the
Service itself, publishing a custom class when bound:

Service提供的方法

public abstract IBinder onBind (Intent intent)
Return the communication channel to the service. 该方法返回一个与service的通信通道,这个方法必须被重写。

public void onCreate ()
Called by the system when the service is first created. Do not call this method directly. service第一次被create时调用。。。不要直接调用该方法

public int onStartCommand (Intent intent, int flags, int startId)
Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly. 每次开启service都要调用该方法

还有一些自己看,也很简单。

《疯狂的Android讲义》在讲解Service这一章时,还附带讲解了TelephonyManager、SmsManager、AudioManager、Vibrator、AlarmManage的功能与用法。我好奇的是,Serviece与这些管理类的关系,该书并没有说明。只是解释说Android系统本身提供了大量的Service组件…莫非这些就是Service组件??

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值