6.关于系统服务的思考—— native vs java

native服务 以sensor service为例

  1. service启动
    1. SystemServer.startBootstrapServices---->>>mSystemServiceManager.startService—>>>SensorService.java构造中startSensorServiceNative
    2. frameworks/base/services/core/jni/
      1. onlod.cpp 中注册
      2. com_android_server_sensor_SensorService.cpp中register_android_server_sensor_SensorService
    3. NativeSensorService::NativeSensorService
      1. sp<IServiceManager> sm(defaultServiceManager())
      2. mService = new SensorService()
      3. sm->addService native service 添加到service manager
  2. ipc
    1. app 与sensor service 通过sensor manager
      1. frameworks/base/core/jni/AndroidRuntime.cpp 中注册 jni
      2. frameworks/base/core/jni/android_hardware_SensorManager.cpp
      3. SensorManager::getService()
        1. sp<IServiceManager> sm = defaultServiceManager();//frameworks/native/libs/binder/include/binder/IServiceManager.h
        2. *outService = interface_cast<INTERFACE>(sm->getService(name));
    2. sensor service 与hal
      1. SensorDevice::connectHalService()
        1. sp<android::hardware::sensors::V2_1::ISensors> sensors =
          android::hardware::sensors::V2_1::ISensors::getService();
          在这里插入图片描述

Native 服务

hal 服务可以直接给app调用
有空再研究

java 服务, 以vibrate为例

  1. 启动
    1. SystemServer.java 中new VibratorService(context)
    2. ServiceManager.addService(“vibrator”, vibrator);
  2. ipc
    1. app 与vibrator service 通过 SystemVibrator.java :
      1. ContextImpl getSystemService 静态函数中new SystemVibrator
      2. SystemVibrator 构造中 mService = IVibratorService.Stub.asInterface(ServiceManager.getService(“vibrator”));
      3. 定义aidl, frameworks/base/core/java/android/os/IVibratorService.aidl
      4. public class VibratorService extends IVibratorService.Stub
    2. vibrator servic 与hal 涉及硬件通信的后续再看

java 服务 app 系统应用 三方应用

java 服务 以一个demo为例

https://blog.nowcoder.net/n/691384d649604fcd8ccb32842e8bf9e1

  1. 启动

    1. frameworks\base\core\java\android\os\IFregService.aidl // 声明 set get
    2. FregService extends IFregService.Stub
      1. 构造中 mPtr = init_native();–>> ipc open设备节点
      2. setVal–>>setVal_native(mPtr, val);
      3. frameworks\base\services\jni\com_android_server_FregService.cpp
      4. freg_init–>> register_android_server_FregService
    3. jni: // frameworks\base\services\jni\com_android_server_FregService.cpp
      1. JNINativeMethod method_table[] register_android_server_FregService {jniRegisterNativeMethods }
    4. SystemServer.java ServiceManager.addService("diskstats", new DiskStatsService(context));
    5. ServiceManager.addService(“freg”, new FregService());
  2. ipc

    1. app与FregService .activity onCreate中

      fregService = IFregService.Stub.asInterface(
      // 获得一个名称为“ freg ”的服务的 Binder 代理对象接口
      ServiceManager.getService(“freg”));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值