关于Android Service的官方文档解释

A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

service 是一个运行在background(应该是和activity相反的状态,就是说service用户是看不到的,相对于activity来说没有提供界面不能和用户交互)的并且执行费时的或是持久的操作的应用程序的组件。一个另外的应用程序的组件可以启动一个服务并且这个服务可以继续保持运行着即使用户切换到另外的一个应用程序。另外一个组件可以绑定一个service并与这个service交互,甚至可以进行进程间交互。举例来说,一个服务可以操作处理网络数据传输或播放音乐或执行IO文件操作抑或是与content provider交互。这些交互都是从后台发出的(就是说这些各式任务都是在用户看不到的后台任务里执行并完成的)。

A service can essentially take two forms:  一个service 本质上有俩种体现形式(或者是有俩种启动方式):

Started

A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.
当一个应用程序组件(这里拿activity举例)在activity里可以通过调用 startService()方法来启动一个service。一旦启动后,这个service就会无限期的运行下去(就是说不听启动它的那个activity的话了)即使启动这个service的组件已经挂掉了。一个被启动的service执行一个单一的操作并且不返回任何结果给调用者(就是说 如果activity启动了这个service 那么service开始后台执行任务 但是不返回任何结果给activity)。举个栗子:一个被启动的service也许在上传或者下载文件,当上传或者下载任务结束后,这个service应该自己停止自己的运行(个人的实际开发理解就是:activity可以启动service也可以停止service,也可以是当service执行完任务后自己停掉自己。)。
 
 
Bound
A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service 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 them unbind, the service is destroyed.
一个应用程序组件通过调用bind Service()方法来启动的service叫做绑定的service。一个绑定的service提供一个“客户端-服务器端”的接口这个接口容许应用程组件与这个service交互,发送请求,返回结果 甚至也可以跨进程实现进程间通信。只要一个应用程序的组件绑定一个service那么这个service就会运行。多个组件可以立刻马上绑定一个service 但是当所有的组件unbind(取消绑定)的时候 这个service就挂了。
 
 

Although this documentation generally discusses these two types 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 whether you implement a couple callback methods: onStartCommand() to allow components to start it and onBind() to allow binding.

上边总得说来就是讨论俩种不同启动sevice的方式,俩种方式都没问题,唯一注意的一点就是实现几个回调方法:onStartCommand()这个方法是容许组件去启动service,onBind()是容许组件绑定service。

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

不管你的程序是started(启动的)还是bound(绑定的)或者俩种都用了,任何应用程序的组件都可以使用这个service(甚至是其他的应用程序的组件也可以启动这个eservice)同理的任何应用程序的组件都可以通过一个intent来使用一个activity。(就是说 intent是跨进程的 可以做到进程间通信和service一样厉害)。然而你可以在manifest里声明一个service成为private用来阻止别的应用程序的使用。这个private的service将会在Declaring the service in the manifest 这部分谈论。

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

注意了留意了:service是运行在宿主进程的主线程(或者叫做UI线程)。service不会创建自己的线程也不会运行在其他的进程里(就是说这个service在哪个进程启动的就运行在哪个进程,除非你特别指定(意思是我们可以特别的指定这个service运行在创建他的这个进程还是其他的进程))。也就是说 如果你的service要处理一些大量需要cpu计算的任务或者大量的阻塞操作(blocking operations)的时候,你应该在该service里创建一个新的线程来做刚才提到的那些操作。通过在service里另外启动一个线程来做耗时费力的操作时你会减少ANP(就是用户界面卡死了 没有任何反应,不能响应用户的任何操作,最终会弹出一个对话框(等待,继续))出现的风险,同时主线程(UI线程)与用户的交互不会出现问题。

 

今天写这个文章拿出官网的说明来分享 就是因为有好多的文章视频讲得 弄得我很迷糊,什么后台进程什么什么的,也许是我真的肤浅理解不了,但是今天的这个官网的说明我觉得真的很实用,最后的Caution:这一段 是对我们实际开发人员来说最有用的。  做android开发快俩年了,注册博客园有一年了但是一直忙于项目也在学些别的东西,一直没有发表东西,惭愧。

转载于:https://www.cnblogs.com/proudToBeAITeer/archive/2013/03/03/2941747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值