美文网首页
Service相关的数据结构

Service相关的数据结构

作者: 泡面先生_Jack | 来源:发表于2020-03-08 19:13 被阅读0次

1. Service的数据结构

service_ams.png

2. LoadedApk

final ArrayMap<Context, ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher>> mServices

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

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<ConnectionRecord> 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<ProcessRecord, AppBindRecord> apps
            = new ArrayMap<ProcessRecord, AppBindRecord>();
    /** 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<Intent.FilterComparison, IntentBindRecord> bindings
            = new ArrayMap<Intent.FilterComparison, IntentBindRecord>();
                            // All active bindings to the service.
    final ArrayMap<IBinder, ArrayList<ConnectionRecord>> connections
            = new ArrayMap<IBinder, ArrayList<ConnectionRecord>>();
                            // 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<ServiceRecord> services = new ArraySet<>();
    // services that are currently executing code (need to remain foreground).
    final ArraySet<ServiceRecord> executingServices = new ArraySet<>();
    // All ConnectionRecord this process holds
    final ArraySet<ConnectionRecord> connections = new ArraySet<>();
  1. services保存了当前进程中所有正在运行的Service信息
  2. executingServices 保存了所有当前进程中正在执行Service生命周期的Service信息
  3. connections 保存了当前进程中所有绑定其他Service的连接信息

相关文章

  • Android bindService流程分析

    1.1 Service相关数据结构 1.2 Service Bind & Unbind流程 2.1 bindSer...

  • Service相关的数据结构

    1. Service的数据结构 2. LoadedApk bindService(intent, serviceC...

  • Service相关

    Service基本用法、Service生命周期、service与Activity通信 一、Service基本用法 ...

  • Service相关

    一、Service 简介 Service 是 Android 程序中四大基础组件之一,它和 Activity一样都...

  • Android Service用法知识点的讲解

    Android Service 学习Service相关知识点: android service 的基础知识,生命周...

  • 关于Android的Service知识点,你知道吗?

    目录 学习Service相关知识点: 概述; Service生命周期; Service的基本用法; 服务。 问:达...

  • 数据库

    mysql服务相关 sudo service mysql start sudo service mysql sto...

  • Service相关知识

    先从生命周期说起吧: 首先他有三个生命周期,分别是:onCreate(),onStartCommand(),onD...

  • 3 Service相关

    service的应用场景,以及和Thread的区别开启service的两种方式以及区别 1、service是什么?...

  • Android Service相关

    Android 的 Service 是四大组件之一,有着非常重要的地位。下面来记录一些重要的知识点。 常用方法 生...

网友评论

      本文标题:Service相关的数据结构

      本文链接:https://www.haomeiwen.com/subject/spardhtx.html