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
Background Service限制
??
Foreground / Background App 定義 ??
App target API 26及以上,如果App不在前臺運行,則系統對正在運行的后臺Services做了一些限制。這種情況下,可以考慮使用Scheduled Job實現; ??
在AndroidManifest.xml文件中該Service的聲明處增加 android:exported=false ??
在proguard文件中keep該Service。 ??
用戶可以看到設備上正在運行的Service。如果用戶不清楚該Service的功能 或者不信任該Service ,可以Stop掉它。為了防止用戶誤殺你應用中的Service,應該在AndroidManifest.xml中聲明該Service的標簽中增加android:description=“xxx”,用于簡單描述該Service的提供的功能及便利。
??
Service默認運行在其所在進程的主線程中;Service并沒有創建自己的線程 或者 運行在別的進程中(除非在標簽中指定了 android:process=“xxx”)。所以如果要在Service中進行耗時操作,一定要創建新線程來執行該操作,否則該耗時操作會阻塞UI Thread,造成ANR。 ??
總結
以上是生活随笔為你收集整理的Services overview的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vim搜索
- 下一篇: 01-JVM与Java体系结构