Android输入事件InputReader和InputDispatcher分析

输入事件分发流程:
1.InputReader 线程通过 EventHub 监听 /dev/input 内的文件
2.InputReader 得到输入事件,封装后存入 InputDispatcher 的队列
3.唤醒 InputDispatcher 
4.InputDispatcher 取出输入事件,并传递给 Window

<1>.C++层  
frameworks/native/services/inputflinger/InputDispatcher.cpp
  KeyEvent event;
  event.initialize(args->deviceId, args->source, args->action,flags, keyCode, args->scanCode, metaState, 0 /*repeatCount默认值为:0;修改值的可以逻辑在java里/,args->downTime, args->eventTime);  
void InputDispatcher::notifyKey(const NotifyKeyArgs* args) {  
  mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags);  
}  
<2>.JNI层
frameworks/base/services/core/jni/com_android_server_input_InputManagerService.cpp  
void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags){  
  env->CallIntMethod(mServiceObj,gServiceClassInfo.interceptKeyBeforeQueueing/*对应java层*/,keyEventObj, policyFlags);  
 }  
<3>java层
frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java  
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {  
  case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:{  
    result &= ~ACTION_PASS_TO_USER; //no return app  
    Log.e(TAG,"KeyCode = " + String.valueOf(event.getKeyCode()) + " action = " + String.valueOf(event.getAction()));  
    if(!down){  
      Log.d(TAG,"DownTime = "+ String.valueOf(event.getDownTime())+ "EventTime = " + String.valueOf(event.getEventTime()));  
      p  
    if((event.getEventTime() - event.getDownTime()) >= 3000) {  //按键大于3s
      Intent mIntent = new Intent();  
      mIntent.putExtra("blue_receive","start");  
      mIntent.setAction("com.test");  
      Log.e(TAG,"send broading...");  

      mContext.sendBroadcast(mIntent,null);  

      Log.e(TAG,"Time > 3s");  
    }else{  
      Log.e(TAG,"Time < 3s");  
    }  
    }  
    break;  
  }  
 }  

参考:http://blog.csdn.NET/u010122827/article/category/5944877
    http://andevele.com/2016/07/07/keypad_first/
    http://blog.csdn.net/u012439416/article/details/54605998

你可能感兴趣的:(Android,framework之积累)