当前位置: 首页 > news >正文

Android SystemServer中Service的创建和启动方式(基于Android13)

Android SystemServer创建和启动方式(基于Android13)

SystemServer 简介

Android System Server是Android框架的核心组件,运行在system_server进程中,拥有system权限。它在Android系统中扮演重要角色,提供服务管理和通信。

system          548    415 1 06:23:21 ?     00:11:21 system_server

SystemServer在Android系统中的位置如下

SystemServer服务提供者serviceManager

SystemServer利用ServiceManager来提供服务,类似于keystore,ServiceManager是一个native service,负责SystemServer中的service管理。SystemServer通过binder和ServiceManager进行通信。

ServiceManager由servicemanager.rc启动,并且相关实现在ServiceManager提供的aidl接口中。

//frameworks/native/cmds/servicemanager/
service servicemanager /system/bin/servicemanagerclass core animationuser systemgroup system readproccriticalonrestart restart healthdonrestart restart zygoteonrestart restart audioserveronrestart restart mediaonrestart restart surfaceflingeronrestart restart inputflingeronrestart restart drmonrestart restart cameraserveronrestart restart keystoreonrestart restart gatekeeperdonrestart restart thermalservicewritepid /dev/cpuset/system-background/tasksshutdown critical

这些接口位于frameworks/native/libs/binder/aidl/android/os/IServiceManager.aidl,主要包括addService、getService、checkService以及一些权限的检查。

//frameworks/native/libs/binder/aidl/android/os/IServiceManager.aidl
interface IServiceManager {/** Must update values in IServiceManager.h*//* Allows services to dump sections according to priorities. */const int DUMP_FLAG_PRIORITY_CRITICAL = 1 << 0;const int DUMP_FLAG_PRIORITY_HIGH = 1 << 1;const int DUMP_FLAG_PRIORITY_NORMAL = 1 << 2;/*** Services are by default registered with a DEFAULT dump priority. DEFAULT priority has the* same priority as NORMAL priority but the services are not called with dump priority* arguments.*/const int DUMP_FLAG_PRIORITY_DEFAULT = 1 << 3;const int DUMP_FLAG_PRIORITY_ALL = 15;// DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_HIGH// | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PRIORITY_DEFAULT;/* Allows services to dump sections in protobuf format. */const int DUMP_FLAG_PROTO = 1 << 4;/*** Retrieve an existing service called @a name from the* service manager.** This is the same as checkService (returns immediately) but* exists for legacy purposes.** Returns null if the service does not exist.*/@UnsupportedAppUsage@nullable IBinder getService(@utf8InCpp String name);/*** Retrieve an existing service called @a name from the service* manager. Non-blocking. Returns null if the service does not* exist.*/@UnsupportedAppUsage@nullable IBinder checkService(@utf8InCpp String name);/*** Place a new @a service called @a name into the service* manager.*/void addService(@utf8InCpp String name, IBinder service,boolean allowIsolated, int dumpPriority);/*** Return a list of all currently running services.*/@utf8InCpp String[] listServices(int dumpPriority);/*** Request a callback when a service is registered.*/void registerForNotifications(@utf8InCpp String name, IServiceCallback callback);/*** Unregisters all requests for notifications for a specific callback.*/void unregisterForNotifications(@utf8InCpp String name, IServiceCallback callback);/*** Returns whether a given interface is declared on the device, even if it* is not started yet. For instance, this could be a service declared in the VINTF* manifest.*/boolean isDeclared(@utf8InCpp String name);/*** Request a callback when the number of clients of the service changes.* Used by LazyServiceRegistrar to dynamically stop services that have no clients.*/void registerClientCallback(@utf8InCpp String name, IBinder service, IClientCallback callback);/*** Attempt to unregister and remove a service. Will fail if the service is still in use.*/void tryUnregisterService(@utf8InCpp String name, IBinder service);
}

在servicemanager启动后,它会注册一个特殊的service,服务名叫做"manager",可以通过dumpsys -l命令找到名为"manager"的服务。

//frameworks/native/cmds/servicemanager/main.cppsp<ServiceManager> manager = new ServiceManager(std::make_unique<Access>());if (!manager->addService("manager", manager, false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()) {LOG(ERROR) << "Could not self register servicemanager";}

原生框架创建Service的几种方式

方式1 ServiceManager.addService

ServiceManager.addService是最早的一种service创建方式,函数原型为

    public static void addService(String name, IBinder service) {addService(name, service, false, IServiceManager.DUMP_FLAG_PRIORITY_DEFAULT);}

在早期的Android版本中,ServiceManager.addService是最早的一种创建service的方式。它的函数原型为ServiceManager.addService,由于存在于早期版本,因此使用起来没有太多限制,甚至在android_app中也可以使用。

方式2 SystemServiceManager.startService

SystemServiceManager.startService有多个override方法,接口定义如下:

    public void startService(@NonNull final SystemService service) {// Register it.mServices.add(service);// Start it.long time = SystemClock.elapsedRealtime();try {service.onStart();} catch (RuntimeException ex) {throw new RuntimeException("Failed to start service " + service.getClass().getName()+ ": onStart threw an exception", ex);}warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");}

SystemService类位于frameworks/base/services/core/java/com/android/server/SystemService.java,最后打包到service.jar中。然而,由于SystemService添加了注解,直接依赖services.jar无法访问该类。

我们可以通过两种方式来使用SystemService:

  • frameworks/base/services内部源码中,可以直接访问SystemService。
  • 在Android.bp中将模块声明为sdk_version: "system_server_current",也可以使用SystemService。

另外,还可以通过依赖静态库services.core来访问SystemService,例如Apex service就是使用这种方式。

http://www.lryc.cn/news/110199.html

相关文章:

  • Meta开源AI音频和音乐生成模型
  • rust怎么解析json数据?
  • STM32 NOR_FLASH 学习
  • 【数据结构|二叉树遍历】递归与非递归实现前序遍历、中序遍历、后序遍历
  • iPhone 8 Plus透明屏有哪些场景化应用?
  • 解决 MySQL 删除数据后,ID 自增不连续问题
  • arcgis--网络分析(理论篇)
  • Linux笔记1(系统状态等)
  • Set-up ESP-AT Environment on Windows using CMD
  • SpringBoot中Redis报错:NOAUTH Authentication required
  • 需求飙升120%!芭比产品火爆出圈,意大利人争相购买!
  • echarts-pie---------3D曲状环形饼图实现!!!
  • 合并两个有序链表(leetcode)
  • CAS之AtomicReference原理解析
  • JS/JQ实现字符串加密成 HEX(十六进制) 字符串
  • 骨传导耳机怎么样?盘点五款适合室外佩戴的骨传导耳机
  • 【flink】使用flink-web-ui提交作业报错
  • 「从零入门推荐系统」22:chatGPT、大模型在推荐系统中的应用
  • 机器学习---概述(一)
  • 概念解析 | AutoFed:面向异构数据的联邦多模态自动驾驶的学习框架
  • vue3+uniapp自定义tabbar
  • stable diffusion webui 安装
  • csdn文章编辑器必备语法备用
  • 机器学习鲁棒性笔记
  • ubuntu 有 1 个软件包没有被完全安装或卸载
  • 【QT调用ST-link-使用QT编写程序-调用ST-LINK_CLI.exe-烧写STM32F4xxx-基础样例】
  • 高并发下的Java项目解决方案
  • 华为推出手机系统云翻新服务:什么是云翻新?如何使用?
  • 修改时间和创建时间的设计问题
  • CentOS 搭建 Harbor 镜像仓库(图文详解)