InputManager启动流程
文章目录
一. InputManagerService的创建
SystemServer.java
private void startOtherServices() {
// ...省略
// 创建InputManagerService对象
inputManager = new InputManagerService(context);
// 将服务添加到SystemManager中
ServiceManager.addService(Context.INPUT_SERVICE, inputManager,
/* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
// 调用start方法开启线程
inputManager.start();
// ... 省略
}
InputManagerService.java
public InputManagerService(Context context) {
this.mContext = context;
this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());
// native初始化
mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());
// ... 省略
}
com_android_server_input_InputManagerService.cpp
static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
// Java端传进来的messagequeue,转化成native的
sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
if (messageQueue == NULL) {
jniThrowRuntimeException(env, "MessageQueue is not initialized.");
return 0;
}
// 创建NativeIputManager
NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
messageQueue->getLooper());
im->incStrong(0);
// 地址返回到Java端保存
return reinterpret_cast<jlong>(im);
}
- 获取Java端MessageQueue
- 创建一个NativeInputManager
- 将NativeInputManager的地址返回,也就是说InputManagerService拥有NativeManager的指针
二. NativeInputManager的创建过程
再看创建NativeInputManager的过程
NativeInputManager::NativeInputManager(jobject contextObj,
jobject serviceObj, const sp<Looper>& looper)</

文章详细阐述了Android系统中InputManager从InputManagerService的创建开始,经过NativeInputManager、EventHub、InputReader和InputDispatcher的构建,直至工作线程启动的完整流程。关键组件EventHub负责读取设备事件,InputReader解析并包装事件,而InputDispatcher则执行事件分发。整个流程涉及到Java层与Native层的交互,以及多个线程的启动与协同工作。

5419

被折叠的 条评论
为什么被折叠?



