Service相关的数据结构

1. Service的数据结构

service_ams.png

2. LoadedApk

final ArrayMap> mServices

bindService(intent, serviceConnection,BIND_AUTO_CREATE);
LoadedApk 中的mServices保存了当前进程bindService的连接信息,以Context为Key保存了当前Context对应的ArrayMap

ServiceConnection 对应了ServiceDispatcher中的IServiceConnection.Stub,是一个Binder的服务端,bindeS ervice的时候会将这个Binder传递给AMS, AMS持有其代理端,也就是ConnectionRecord中的conn变量

3. ConnectionRecord

/**
 * Description of a single binding to a service.
 */
final class ConnectionRecord {
    final AppBindRecord binding;    // The application/service binding.
    final ActivityRecord activity;  // If non-null, the owning activity.
    final IServiceConnection conn;  // The client connection.

ConnectionRecord描述了一个bindService的连接
conn : IServiceConnection在AMS中的代理端
activity : 绑定Service的Activity

4. AppBindRecord

/**
 * An association between a service and one of its client applications.
 */
final class AppBindRecord {
    final ServiceRecord service;    // The running service.
    final IntentBindRecord intent;  // The intent we are bound to.
    final ProcessRecord client;     // Who has started/bound the service.

    final ArraySet connections = new ArraySet<>();
                                    // All ConnectionRecord for this client.

记录了一个Service与其关联的Client信息
client : 记录了绑定当前Service的客户端进程的信息
connections: 记录了某一个客户端进程与当前Service的所有bind连接信息

一个进程中可能会有多个地方绑定同一个Service,所以一个进程会有多个ConnectionRecord信息

5. IntentBindRecord


/**
 * A particular Intent that has been bound to a Service.
 */
final class IntentBindRecord {
    /** The running service. */
    final ServiceRecord service;
    /** The intent that is bound.*/
    final Intent.FilterComparison intent; // 
    /** All apps that have bound to this Intent. */
    final ArrayMap apps
            = new ArrayMap();
    /** Binder published from service. */
    IBinder binder;

绑定一个Service的时候需要一个intent参数,所以IntentBindRecord就是来记录一个Intent与其对应的Service的关系

apps: 由于同一个intent,可能有多个不同的进程来bind该service,故采用其成员变量apps来记录使用该intent绑定Service的所有client进程.

binder : service发布过程,调用publishServiceLocked()来赋值的IBinder对象;也就是bindService后的onBinder()方法 的返回值(作target进程的binder服务)的代理对象。简单来说就是onServiceConnected()的第二个参数。

6. ServiceRecord

/**
 * A running application service.
 */
final class ServiceRecord extends Binder implements ComponentName.WithComponentName {
    final ActivityManagerService ams;
    final ComponentName name; // service component.
    final String shortName; // name.flattenToShortString().
    final Intent.FilterComparison intent;
                            // original intent used to find service.
    final ServiceInfo serviceInfo;
                            // all information about the service.
    ApplicationInfo appInfo;
                            // information about service's app.
    final int userId;       // user that this service is running as
    final String packageName; // the package implementing intent's component
    final String processName; // process where this component wants to run
    final Runnable restarter; // used to schedule retries of starting the service
    final long createRealTime;  // when this service was created
    final ArrayMap bindings
            = new ArrayMap();
                            // All active bindings to the service.
    final ArrayMap> connections
            = new ArrayMap>();
                            // IBinder -> ConnectionRecord of all bound clients

    ProcessRecord app;      // where this service is running or null.
    ProcessRecord isolatedProc; // keep track of isolated process, if requested
    boolean isForeground;   // is service currently in foreground mode?
    int foregroundId;       // Notification ID of last foreground req.
    Notification foregroundNoti; // Notification record of foreground state.

ServiceRecord记录了AMS中某一个运行的Service信息,是AMS管理Service的基本单位, ServiceRecord继承于Binder对象,作为Binder IPC的Bn端;Binder将其传递到Service进程的Bp端, 保存在Service.mToken, 即ServiceRecord的代理对象。

  1. ServiceInfo : 记录了AndroidManifest.xml中Service相关的信息
  2. userId : 当前Service是那个user启动的
  3. app : 记录了当前Service所属进程的相关信息
  4. isForeground : 记录了当前Service是否是一个前台进程
  5. bindings : 一个Service可以被其他的进程用不同的Intent来绑定,所以bindings记录了绑定在当前Service上的所有的Intent以及气对应的IntentBindRecord信息.

总结:一个Service可以被其他进程用不同的Intent来绑定
使用同一个Intent绑定Service的可能是多个不同的进程
每个进程中有可能有多个地方bind这个服务,所以有多个Connection连接


Service_关系.png
  1. connections:记录IServiceConnection以及所有对应client的ConnectionRecord
  2. startRequested:代表service是否由startService方式所启动的
  • startService(),则startRequested=true;
  • stopService()或stopSelf(),则startRequested=false;

8. ActiveServices

ActiveServices是AMS中负责管理Service的类, 他变量mService是ServiceMap类型, ServiceMap中保存了运行的Service信息ServiceRecord.
有两种保存方式

  1. mServicesByName: 以CompomentName为key保存了ServiceReocord
  2. mServicesByIntent: 以Intent为Key保存了ServiceRecord

mPendingService: 表示正在等待进程启动的ServiceRecord信息

9. ProcessRecord

    // all ServiceRecord running in this process
    final ArraySet services = new ArraySet<>();
    // services that are currently executing code (need to remain foreground).
    final ArraySet executingServices = new ArraySet<>();
    // All ConnectionRecord this process holds
    final ArraySet connections = new ArraySet<>();
  1. services保存了当前进程中所有正在运行的Service信息
  2. executingServices 保存了所有当前进程中正在执行Service生命周期的Service信息
  3. connections 保存了当前进程中所有绑定其他Service的连接信息

你可能感兴趣的:(Service相关的数据结构)