资源描述:
《android事件处理分析》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、按键事件对于按键事件,调用mDevices->layoutMap->map进行映射。映射实际是由KeyLayoutMap::map完成的,KeyLayoutMap类里读取配置文件qwerty.kl,由配置文件qwerty.kl决定键值的映射关系。你可以通过修改./development/emulator/keymaps/qwerty.kl来改变键值的映射关系。JNI函数在frameworks/base/services/jni/com_android_server_KeyInputQueue.cpp文件中,向JAVA提供了函数android_server_KeyInputQueue_readE
2、vent,用于读取输入设备事件。C代码:staticjbooleanandroid_server_KeyInputQueue_readEvent(JNIEnv*env,jobjectclazz,jobjectevent){gLock.lock();sphub=gHub;if(hub==NULL){hub=newEventHub;gHub=hub;}gLock.unlock();int32_tdeviceId;int32_ttype;int32_tscancode,keycode;uint32_tflags;int32_tvalue;nsecs_twhen;boolres=hub->getEve
3、nt(&deviceId,&type,&scancode,&keycode,&flags,&value,&when);env->SetIntField(event,gInputOffsets.mDeviceId,(jint)deviceId);env->SetIntField(event,gInputOffsets.mType,(jint)type);env->SetIntField(event,gInputOffsets.mScancode,(jint)scancode);env->SetIntField(event,gInputOffsets.mKeycode,(jint)keycode)
4、;env->SetIntField(event,gInputOffsets.mFlags,(jint)flags);env->SetIntField(event,gInputOffsets.mValue,value);env->SetLongField(event,gInputOffsets.mWhen,(jlong)(nanoseconds_to_milliseconds(when)));returnres;}readEvent调用hub->getEvent读了取事件,然后转换成JAVA的结构。事件中转线程在frameworks/base/services/java/com/android/
5、server/KeyInputQueue.java里创建了一个线程,它循环的读取事件,然后把事件放入事件队列里。Java代码:ThreadmThread=newThread("InputDeviceReader"){publicvoidrun(){android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);try{RawInputEventev=newRawInputEvent();while(true){InputDevicedi;//block,doesn'treleaset
6、hemonitorreadEvent(ev);booleansend=false;booleanconfigChanged=false;if(false){Log.i(TAG,"Inputevent:dev=0x"+Integer.toHexString(ev.deviceId)+"type=0x"+Integer.toHexString(ev.type)+"scancode="+ev.scancode+"keycode="+ev.keycode+"value="+ev.value);}if(ev.type==RawInputEvent.EV_DEVICE_ADDED){synchronize
7、d(mFirst){di=newInputDevice(ev.deviceId);mDevices.put(ev.deviceId,di);configChanged=true;}} ...... } } }};按键、触摸屏流、轨迹球程分析输入事件分发线程在frameworks/base/services/java/com/andro