InputManager启动流程

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

InputManager启动流程

InputReader源码分析

InputDispather源码分析

一. 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);
}
  1. 获取Java端MessageQueue
  2. 创建一个NativeInputManager
  3. 将NativeInputManager的地址返回,也就是说InputManagerService拥有NativeManager的指针
二. NativeInputManager的创建过程

再看创建NativeInputManager的过程

NativeInputManager::NativeInputManager(jobject contextObj,
        jobject serviceObj, const sp<Looper>& looper)</
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无敌的娃哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值