文章目录
- native服务 以sensor service为例
- Native 服务
- java 服务, 以vibrate为例
- java 服务 以一个demo为例
native服务 以sensor service为例
- service启动
- SystemServer.startBootstrapServices---->>>mSystemServiceManager.startService—>>>SensorService.java构造中startSensorServiceNative
- frameworks/base/services/core/jni/
- onlod.cpp 中注册
- com_android_server_sensor_SensorService.cpp中register_android_server_sensor_SensorService
- NativeSensorService::NativeSensorService
- sp<IServiceManager> sm(defaultServiceManager())
- mService = new SensorService()
- sm->addService native service 添加到service manager
- ipc
- app 与sensor service 通过sensor manager
- frameworks/base/core/jni/AndroidRuntime.cpp 中注册 jni
- frameworks/base/core/jni/android_hardware_SensorManager.cpp
- SensorManager::getService()
- sp<IServiceManager> sm = defaultServiceManager();//frameworks/native/libs/binder/include/binder/IServiceManager.h
- *outService = interface_cast<INTERFACE>(sm->getService(name));
- sensor service 与hal
- SensorDevice::connectHalService()
- sp<android::hardware::sensors::V2_1::ISensors> sensors =
android::hardware::sensors::V2_1::ISensors::getService();
- sp<android::hardware::sensors::V2_1::ISensors> sensors =
- SensorDevice::connectHalService()
- app 与sensor service 通过sensor manager
Native 服务
hal 服务可以直接给app调用
有空再研究
java 服务, 以vibrate为例
- 启动
- SystemServer.java 中new VibratorService(context)
- ServiceManager.addService(“vibrator”, vibrator);
- ipc
- app 与vibrator service 通过 SystemVibrator.java :
- ContextImpl getSystemService 静态函数中new SystemVibrator
- SystemVibrator 构造中 mService = IVibratorService.Stub.asInterface(ServiceManager.getService(“vibrator”));
- 定义aidl, frameworks/base/core/java/android/os/IVibratorService.aidl
- public class VibratorService extends IVibratorService.Stub
- vibrator servic 与hal 涉及硬件通信的后续再看
- app 与vibrator service 通过 SystemVibrator.java :
java 服务 app 系统应用 三方应用
java 服务 以一个demo为例
https://blog.nowcoder.net/n/691384d649604fcd8ccb32842e8bf9e1
-
启动
- frameworks\base\core\java\android\os\IFregService.aidl // 声明 set get
- FregService extends IFregService.Stub
- 构造中 mPtr = init_native();–>> ipc open设备节点
- setVal–>>setVal_native(mPtr, val);
- frameworks\base\services\jni\com_android_server_FregService.cpp
- freg_init–>> register_android_server_FregService
- jni: // frameworks\base\services\jni\com_android_server_FregService.cpp
- JNINativeMethod method_table[] register_android_server_FregService {jniRegisterNativeMethods }
- SystemServer.java ServiceManager.addService(
"diskstats"
, new DiskStatsService(context)); - ServiceManager.addService(“freg”, new FregService());
-
ipc
-
app与FregService .activity onCreate中
fregService = IFregService.Stub.asInterface(
// 获得一个名称为“ freg ”的服务的 Binder 代理对象接口
ServiceManager.getService(“freg”));
-