andriod-api翻译(二)--Services

来源:互联网 发布:数据库营销成功案例 编辑:程序博客网 时间:2024/06/05 19:21

Services

Service is an application component that can performlong-running operations in the background and does not provide a userinterface. Another application component can start a service and it willcontinue to run in the background even if the user switches to anotherapplication. Additionally, a component can bind to a service to interact withit and even perform interprocess communication (IPC). For example, a servicemight handle network transactions, play music, perform file I/O, or interactwith a content provider, all from the background.

service是可供后台执行长时间运行的操作的应用程序组件,但其不提供UI界面。即使用户切换到另一个程序,此程序仍可以启动一个service,继续在后台运行。此外,组件可以将其绑定到一个service与它交互,以及执行进程间通信(IPC)。例如,service可以在后台处理网络交易,播放音乐,执行I / O文件,或与内容交互。

A service can essentially take two forms:

service基本上可以有两种形式:

Started

A service is "started" when an applicationcomponent (such as an activity) starts it by calling startService(). Once started, a service can run in the backgroundindefinitely, even if the component that started it is destroyed. Usually, astarted service performs a single operation and does not return a result to thecaller. For example, it might download or upload a file over the network. Whenthe operation is done, the service should stop itself.

Started(启动)

当应用程序(如一个activity)启动service时,需要调用startService()方法。一旦启动,service可以一直在后台运行,即使启动它的组件被摧毁。通常,运行中的service执行单一的操作,不用向调用者返回一个结果。例如,它可能是在网络上下载或上传文件。当操作完成,service会自行停止。

Bound

A service is "bound" when an applicationcomponent binds to it by calling bindService(). A bound service offers a client-server interface thatallows components to interact with the service, send requests, get results, andeven do so across processes with interprocess communication (IPC). A boundservice runs only as long as another application component is bound to it.Multiple components can bind to the service at once, but when all of themunbind, the service is destroyed.

Bound(绑定)

应用程序可调用startService()方法将其绑定到service绑定service可提供一个客户端service的接口,允许组件与service交互,发送请求,得到结果,甚至跨进程与进程间通信(IPC)。绑定service后程序会一直运行直到另一个组件绑定到service。多个组件可以同时绑定到service,但当他们全部解绑时,service才被摧毁。

Although this documentation generally discusses these twotypes of services separately, your service can work both ways—it can be started(to run indefinitely) and also allow binding. It's simply a matter of whetheryou implement a couple callback methods: onStartCommand() to allow components to start it andonBind() to allow binding.

此文档讨论了这两种形式的service,但在实际运行时,可以同时使用这两者方式——可被启动(一直运行),也允许绑定。onStartCommand()用来启动,onBind()允许绑定。

Regardless of whether your application is started, bound,or both, any application component can use the service (even from a separateapplication), in the same way that any component can use an activity—bystarting it with an Intent. However, you can declare the service as private, in themanifest file, and block access from other applications. This is discussed morein the section about Declaring theservice in the manifest.

无论应用程序是启动,绑定,或者两者兼有,任何应用程序组件都可以使用service(甚至是一个独立的应用程序),同样的,可以通过启动intent从而实现组件开启activity。然而,可以在manifest文件中声明serviceprivate,以阻止访问其他应用程序。在Declaring the service in the manifest中将会有更多讨论。

Caution: Aservice runs in the main thread of its hosting process—the service does not createits own thread and doesnot run in a separate process (unless youspecify otherwise). This means that, if your service is going to do any CPUintensive work or blocking operations (such as MP3 playback or networking), youshould create a new thread within the service to do that work. By using aseparate thread, you will reduce the risk of Application Not Responding (ANR)errors and the application's main thread can remain dedicated to userinteraction with your activities.

注意: service在主线程中运行,但service不会创建自己的线程, 也不会运行在独立的进程(除非特意指定)。这意味着,如果service将做任何CPU集中型工作或大块数据的操作(如MP3播放或网络工作),应该在service中创建一个新线程去工作。通过独立的线程,将会减少应用程序无响应(ANR)错误的风险,应用程序的主线程仍然可以与用户交互。

 

Should you use a serviceor a thread?

A service is simply a componentthat can run in the background even when the user is not interacting with yourapplication. Thus, you should create a service only if that is what you need.

If you need to perform workoutside your main thread, but only while the user is interacting with yourapplication, then you should probably instead create a new thread and not aservice. For example, if you want to play some music, but only while youractivity is running, you might create a thread in onCreate(),start running it inonStart(),then stop it in onStop().Also consider using AsyncTask orHandlerThread,instead of the traditional Thread class.See theProcessesand Threading documentfor more information about threads.

Rememberthat if you do use a service, it still runs in your application's main threadby default, so you should still create a new thread within the service if itperforms intensive or blocking operations.

 

应该使用service还是线程?

 

Service是可以在后台运行的组件,即使用户与应用程序没有交互。因此,当需要时可以创建一个service以满足要求。

 

如果需要执行主线程外的工作,且用户与应用程序有交互,那么应该是创建一个新的线程,而不是service。例如,如果想播放音乐,而activity正在运行,此时可以调用onCreate()创建一个线程,调用onStart()进行启动,调用onStop ()来停止。也可以考虑使用AsyncTask 或则HandlerThread,而不是传统的Thread类。关于更多线程的信息请参见the Processesand Threading文件。

 

service在默认情况下仍然运行在应用程序的主线程上,所以在执行集中型工作或大块数据的操作时还是应该在service上创建在一个新的线程。

0 0
原创粉丝点击