一、Service介绍
Service是一个可以在后台执行长时间运行操作而不提供用户界面的应用组件。服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。 此外,组件可以绑定到服务,以与之进行交互,甚至是执行进程间通信 (IPC)。 例如,服务可以处理网络事务、播放音乐,执行文件 I/O 或与内容提供程序交互,而所有这一切均可在后台进行
Service分类
按照启动方式可以分为:
1、Started Service
当组件通过调用 startService() 方法启动 Service ,这时,Service处于启动状态,可以在后台无限期的运行,只有自身调用stopSelf() 方法 或者 其他组件调用 stopService() 方法才会停止Service。
2、Bound Service
当组件通过调用 bindService() 方法绑定 Service,这时,Service处于绑定状态,多个组件可以绑定在一个Service上,当绑定Service的所有组件解绑之后,该Service被销毁。
两种启动方式的生命周期如下:
二、Android Service相关接口和类
IServiceConnection
IServiceConnection是Android中的一个接口,用于管理应用程序组件与服务之间的连接。
IServiceConnection文件路径:
frameworks/base/core/java/android/app/IServiceConnection.aidl
IServiceConnection定义:
oneway interface IServiceConnection {}
IServiceConnection方法:
void connected(in ComponentName name, IBinder service, boolean dead);
Service
Service类是Android中的一个重要组件,用于在后台执行长时间运行的操作,而不需要与用户界面进行交互。各个Service类必须是Service类的子类,并且需要重写一些处理服务生命周期的重要回调方法。
Service文件路径:
frameworks/base/core/java/android/app/Service.java
Service定义:
public abstract class Service extends ContextWrapper implements ComponentCallbacks2,
ContentCaptureManager.ContentCaptureClient {}
Service 方法:
final Application getApplication():返回拥有此服务的应用程序。
abstract IBinder onBind(Intent intent):将通信信道返回给服务。
void onConfigurationChanged(Configuration newConfig):设备配置在组件运行时发生更改时由系统调用。
void onCreate():服务第一次创建时由系统调用。
void onDestroy():由系统调用以通知服务它已不再使用并正在被删除。
void onLowMemory():这在整个系统内存不足时调用,并且主动运行的进程应该修剪内存使用情况。
void onRebind(Intent intent):当新的客户端连接到服务时调用,之前它已被通知所有已断开其 onUnbind(Intent) 。
void onStartCommand(Intent intent, int flags, int startId):每次客户端通过调用 startService(Intent)显式启动服务时,由系统调用,提供它提供的参数和表示启动请求的唯一整数标记。
void onTaskRemoved(Intent rootIntent):如果服务当前正在运行并且用户已经删除了来自服务应用程序的任务,则会调用该服务。
void onTrimMemory(int level):当操作系统确定进程从其进程中删除不需要的内存是一个好时机时调用。
boolean onUnbind(Intent intent):当所有客户端与服务发布的特定接口断开连接时调用。
final void startForeground(int id, Notification notification):用于将服务设置为前台服务。前台服务是一种在通知栏显示持续通知的服务,通常用于执行用户可见的任务,例如播放音乐或下载文件等。
final void startForeground(int id, Notification notification, int foregroundServiceType) :用于将服务设置为前台服务。
final void stopForeground(int flags):从前台状态中删除此服务,如果需要更多内存,则允许它被终止。
final void stopForeground(boolean removeNotification):从前台状态中删除此服务,如果需要更多内存,则允许它被终止。
final int getForegroundServiceType() :获取此服务的前台状态。
final void stopSelf():停止服务,如果它以前开始。
final boolean stopSelfResult(int startId):如果最近一次启动的时间是 startId,请停止服务。
ServiceConnection
Interface for monitoring the state of an application service.
用于监视应用程序服务状态的接口。
ServiceConnection文件路径:
frameworks/base/core/java/android/content/ServiceConnection.aidl
ServiceConnection定义:
public interface ServiceConnection {}
ServiceConnection方法:
void onServiceConnected(ComponentName name, IBinder service);
void onServiceDisconnected(ComponentName name);
LocalServices
This class is used in a similar way as ServiceManager, except the services registered here are not Binder objects and are only available in the same process.
此类的使用方式与 ServiceManager 类似,只是此处注册的服务不是 Binder 对象,并且仅在同一进程中可用。
LocalServices文件路径:
frameworks/base/core/java/com/android/server/LocalServices.java
LocalServices定义:
public final class LocalServices {}
LocalServices方法:
public static <T> T getService(Class<T> type):Returns a local service instance that implements the specified interface.
public static <T> void addService(Class<T> type, T service):Adds a service instance of the specified interface to the global registry of local services.
public static <T> void removeServiceForTest(Class<T> type):Remove a service instance, must be only used in tests.
LocalManagerRegistry
The registry for in-process module interfaces.
进程内模块接口的注册表。
LocalManagerRegistry文件路径:
frameworks/base/services/core/java/com/android/server/LocalManagerRegistry.java
LocalManagerRegistry定义:
public final class LocalManagerRegistry {}
LocalManagerRegistry方法:
public static <T> T getManager(@NonNull Class<T> managerClass):Get a manager from the registry.
public static <T> void addManager(@NonNull Class<T> managerClass, @NonNull T manager):Adds a manager to the registry.
三、Service相关流程分析
Service attach流程分析
Android13 Service attach流程分析-CSDN博客
Service stopSelf流程分析
Android13 Service stopSelf流程分析-CSDN博客
Service startForeground流程分析
Android13 Service startForeground流程分析-CSDN博客