Reference/android/app/Service
From Android中文网
android.app 公有抽象类
目录 |
[编辑] android.app.Service
java.lang.Object
android.content.Context android.app.ApplicationContext android.app.Service
[编辑] Service 介绍
服务是在后台长时间运行的应用组件,不和用户直接进行交互。在每一个服务类的包的 AndroidManifest.xml 文件中,必须有一个相应的 <service> 声明。服务必须用 Context.startService() 或者 Context.bindService() 启动。
和其它应用对象一样,服务运行在它们宿主进程的主线程里。这意味着,如果一个服务需要执行阻塞操作(比如网络操作)或者 CPU 敏感的操作(比如 MP3播放器),它应该分离出一个线程来执行这样的操作。
服务类是应用程序的生命周期中的一个重要部分。
在这里要讨论的内容有:
- 服务的生命周期
- 访问权限
- 进程生命周期
[编辑] 服务的生命周期
启动服务有两种方法。
如果客户调用 Context.startService(),系统将获得服务(如果服务不存在,系统创建服务,然后调用它的 onCreate() 方法),然后使用调用者提供的参数调用服务的 onStart(int, Bundle) 方法。从此以后,服务开始持续运行,直到 Context.stopService() 或者 stopSelf() 被调用。注意:多次调用 Context.startService() 虽然会导致 onStart() 被多次调用,但是服务本身不会嵌套(原文如此,应该是说服务的实例只有一个,不会启动多个服务)。所以无论调用多少次 Context.startService(),只要调用一次 Context.stopService() 或者 stopSelf(),服务就会停止运行。
客户也可以调用 Context.bindService() 获得到服务的永久连接。如果服务之前没有启动,一样会创建服务然后调用它的 onCreate() 方法;但是不会调用它的 onStart() 方法。服务调用它的 getBinder() 方法,并且将返回的 IBinder 对象传递给客户。连接建立以后,不管客户是否保留这个 IBinder 对象的引用,只要连接还存在,服务都会持续运行。通常返回的 IBinder 对象是一个由 AIDL 实现的复杂接口。
服务可以同时被启动和绑定多个连接。在这种情况下,只要服务被启动,或者存在着到这个服务的连接,服务都会持续运行。当两个条件都不满足时,系统调用服务的 onDestroy() 方法,服务从此被终止。当 onDestroy() 返回的时候,所有的清理工作(停止线程,取消已经注册的 receivers)都已经完成。
[编辑] 访问权限
对服务的全局访问权限可以通过服务的 manifest 中的 <service> 元素指定。这样,其它应用需要在它们的 manifest 中声明对应的 <uses-permission> 元素,这样才能启动、停止和绑定到服务。
同时,在执行 IPC 调用之前,服务可以调用 checkCallingPermission(String) 对这次 IPC 调用的权限进行检查。
关于权限和安全方面的信息,请参考安全模型文档。
[编辑] 进程生命周期
只要服务被启动或者被客户绑定(建立连接),Android 系统就尽可能维护一个进程来作这个服务的宿主。当系统内存不足的时候,系统需要杀死进程来出让内存。这时候在下列情况下,服务的宿主进程具有较高的优先级:
如果服务已经被启动,它的宿主进程比任何在屏幕上对用户可见的进程都具有更低的优先级;但是比其它所有不可见的进程都具有更高的优先级。通常对用户可见的进程的数量非常少,所以正在运行的服务在绝大多数时候不会被杀死 —— 除非系统的可用内存极其匮乏。
如果有客户绑定在服务上,服务的宿主进程的优先级至少和客户的优先级一样(不会比客户更低)。这意味着如果客户对用户可见,那么服务本身也会被系统认为对用户可见。
在服务的宿主进程中运行有其它应用组件,比如 activity,可以提高整个进程的优先级,而不是仅仅提高服务本身的优先级。
[编辑] 概要
[编辑] 常量
| String | TAG | "Service" |
[编辑] 从类 android.content.Context 继承的常量:
ALARM_SERVICE, BIND_AUTO_CREATE, CONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, INFLATE_SERVICE, KEYGUARD_SERVICE, LOCATION_SERVICE, MODE_APPEND, MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, NOTIFICATION_SERVICE, POWER_SERVICE, WINDOW_SERVICE
[编辑] 公共构造函数
Service()
| Service() |
[编辑] 公共成员函数
| final | Application getApplication() | 返回拥有这个服务的应用。 |
| abstract | IBinder getBinder() | 返回到这个服务的通信通道 (communication channel)。 |
| final | void stopSelf(int startId) | 停止服务,如果它最后一次启动的 ID 是 startID。 |
| final | void stopSelf() | 如果服务已经启动,则停止服务。 |
[编辑] 保护成员函数
| void onCreate() | 在服务第一次被创建的时候被调用。 |
| void onDestroy() | 在服务不再被使用,需要被删除时被调用。 |
| void onStart(int startId, Bundle arguments) | 客户调用 startService(Intent, Bundle) 直接启动服务的时候被调用。Bundle 是客户提供的参数,startID 是这次服务启动的唯一标识。 |
[编辑] 从类 android.app.ApplicationContext 继承的方法:
applyThemeResource, bindService, broadcastIntent, broadcastIntent, broadcastIntent, checkCallingOrSelfPermission, checkCallingPermission, checkPermission, clearWallpaper, closeExternalStorageFiles, createDatabase, createPackageContext, deleteDatabase, deleteFile, fileList, getAssets, getClassLoader, getContentResolver, getDataDir, getFileStreamPath, getPackageManager, getPackageName, getPackagePath, getResources, getSharedPreferences, getSystemService, getTheme, getWallpaper, openDatabase, openFileInput, openFileOutput, peekWallpaper, registerExternalStorageListener, registerReceiver, registerReceiver, setTheme, setWallpaper, setWallpaper, showAlert, showAlert, showAlert, showAlert, startActivity, startInstrumentation, startService, stopService, unbindService, unregisterReceiver
[编辑] 从类 android.content.Context 继承的方法:
bindService, broadcastIntent, broadcastIntent, broadcastIntent, checkCallingOrSelfPermission, checkCallingPermission, checkPermission, clearWallpaper, createDatabase, createPackageContext, deleteDatabase, deleteFile, fileList, getAssets, getClassLoader, getContentResolver, getDataDir, getFileStreamPath, getPackageManager, getPackageName, getResources, getSharedPreferences, getString, getSystemService, getText, getTheme, getWallpaper, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, obtainStyledAttributes, openDatabase, openFileInput, openFileOutput, peekWallpaper, registerReceiver, registerReceiver, setTheme, setWallpaper, setWallpaper, showAlert, showAlert, showAlert, showAlert, startActivity, startInstrumentation, startService, stopService, unbindService, unregisterReceiver
[编辑] 从类 java.lang.Object 继承的方法:
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
[编辑] 详细资料
[编辑] 常数
[编辑] public static final String TAG
常数值: "Service"
[编辑] 公共构造函数
[编辑] public Service()
无
[编辑] 公共成员函数
[编辑] public final Application getApplication()
返回拥有这个服务的应用。
[编辑] public abstract IBinder getBinder()
返回到服务的通信通道 (communication channel)。如果客户没有绑定到服务,返回 null。 IBinder 通常是一个复杂接口(参见 AIDL)。
返回值 返回一个 IBinder 对象,客户可以通过它调用服务(的功能)。
[编辑] public final void stopSelf(int startId)
如果服务最后一次启动的 ID 是 startID,则停止服务。这个函数和调用 stopService(Intent) 有同样的结果,但是如果客户发起了新的启动请求,而请求还没有进入到 onStart(int, Bundle),这个函数可以避免停止服务。
参数 startId: 最后一次启动时在 onStart(int, Bundle) 中收到的启动标识 (ID)。
参考 stopSelf()
[编辑] public final void stopSelf()
如果服务已经被启动,则停止服务,和 stopService(Intent) 有同样的效果。 参见 stopSelf(int)
[编辑] 保护成员函数
[编辑] protected void onCreate()
在服务第一次被创建时被调用。
[编辑] protected void onDestroy()
服务不再被使用而且需要被删除的时候被调用。服务在这里清理所有它占有的资源,包括线程、已经注册的 receivers 等。函数返回后,不应该再调用这个服务的任何方法。事实上,服务已经中止。
[编辑] protected void onStart(int startId, Bundle arguments)
在客户每次调用 startService(Intent, Bundle) 直接启动服务的时候被调用。
参数 startId: 表示这次启动请求的整数标识。可以用在 stopSelf(int) 中。 arguments: 客户在 startService(Intent, Bundle) 中提供的 Bundle。可以为空。
参见 stopSelf(int)
