Services overview

原文链接:https://developer.android.com/guide/components/services

Services overview

A Service is an application component that can perform long-running operations in the background, and it doesn’t provide a user interface. Another application component can start a service, and it continues 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 can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

These are the three different types of services:

Foreground

A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn’t interacting with the app.

Background

A background service performs an operation that isn’t directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.

Note: If your app targets API level 26 or higher, the system imposes restrictions on running background services 1when the app itself isn’t in the foreground2. In most cases like this, your app should use a scheduled job instead.3

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, receive 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.
Although this documentation generally discusses started and bound 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 of callback methods:onStartCommand() to allow components to start it and onBind() to allow binding.

Regardless of whether your service 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.4 This is discussed more in the section about Declaring the service in the manifest.

There are other attributes that you can include in the element to define properties such as the permissions that are required to start the service and the process in which the service should run. The android:name attribute is the only required attribute—it specifies the class name of the service.After you publish your application, leave this name unchanged to avoid the risk of breaking code due to dependence on explicit intents to start or bind the service (read the blog post, Things That Cannot Change).5

Note: Users can see what services are running on their device. If they see a service that they don’t recognize or trust, they can stop the service. In order to avoid having your service stopped accidentally by users, you need to add the android:description attribute to the element in your app manifest. 6 In the description, provide a short sentence explaining what the service does and what benefits it provides.

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. If your service is going to perform any CPU-intensive work or blocking operations, such as MP3 playback or networking, you should create a new thread within the service to complete that work. By using a separate thread, you can reduce the risk of Application Not Responding (ANR) errors, and the application’s main thread can remain dedicated to user interaction with your activities.7


  1. Background Service限制
    Background Service限制 ↩︎

  2. Foreground / Background App 定义Foreground / Background App 定义 ↩︎

  3. App target API 26及以上,如果App不在前台运行,则系统对正在运行的后台Services做了一些限制。这种情况下,可以考虑使用Scheduled Job实现; ↩︎

  4. 在AndroidManifest.xml文件中该Service的声明处增加 android:exported=false ↩︎

  5. 在proguard文件中keep该Service。 ↩︎

  6. 用户可以看到设备上正在运行的Service。如果用户不清楚该Service的功能 或者不信任该Service ,可以Stop掉它。为了防止用户误杀你应用中的Service,应该在AndroidManifest.xml中声明该Service的标签中增加android:description=“xxx”,用于简单描述该Service的提供的功能及便利。
    在这里插入图片描述 ↩︎

  7. Service默认运行在其所在进程的主线程中;Service并没有创建自己的线程 或者 运行在别的进程中(除非在标签中指定了 android:process=“xxx”)。所以如果要在Service中进行耗时操作,一定要创建新线程来执行该操作,否则该耗时操作会阻塞UI Thread,造成ANR。 ↩︎

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值