Android编程输入事件流程详解

所属分类: 软件编程 / Android 阅读数: 27
收藏 0 赞 0 分享

本文实例讲述了Android编程输入事件流程。分享给大家供大家参考,具体如下:

EventHub对输入设备进行了封装。输入设备驱动程序对用户空间应用程序提供一些设备文件,这些设备文件放在/dev/input里面。

EventHub扫描/dev/input下所有设备文件,并打开它们。

bool EventHub::openPlatformInput(void)
{
...
  mFDCount = 1;
  mFDs = (pollfd *)calloc(1, sizeof(mFDs[0]));
  mDevices = (device_t **)calloc(1, sizeof(mDevices[0]));
  mFDs[0].events = POLLIN;
  mDevices[0] = NULL;
  res = scan_dir(device_path);
...
  return true;
}

EventHub对外提供了一个函数用于从输入设备文件中读取数据。

bool EventHub::getEvent(int32_t* outDeviceId, int32_t* outType,
    int32_t* outScancode, int32_t* outKeycode, uint32_t *outFlags,
    int32_t* outValue, nsecs_t* outWhen)
    {
     ...
      while(1) {
    // First, report any devices that had last been added/removed.
    if (mClosingDevices != NULL) {
      device_t* device = mClosingDevices;
      LOGV("Reporting device closed: id=0x%x, name=%s\n",
         device->id, device->path.string());
      mClosingDevices = device->next;
      *outDeviceId = device->id;
      if (*outDeviceId == mFirstKeyboardId) *outDeviceId = 0;
      *outType = DEVICE_REMOVED;
      delete device;
      return true;
    }
    if (mOpeningDevices != NULL) {
      device_t* device = mOpeningDevices;
      LOGV("Reporting device opened: id=0x%x, name=%s\n",
         device->id, device->path.string());
      mOpeningDevices = device->next;
      *outDeviceId = device->id;
      if (*outDeviceId == mFirstKeyboardId) *outDeviceId = 0;
      *outType = DEVICE_ADDED;
      return true;
    }
    release_wake_lock(WAKE_LOCK_ID);
    pollres = poll(mFDs, mFDCount, -1);
    acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_ID);
    if (pollres <= 0) {
      if (errno != EINTR) {
        LOGW("select failed (errno=%d)\n", errno);
        usleep(100000);
      }
      continue;
    }
    for(i = 1; i < mFDCount; i++) {
      if(mFDs[i].revents) {
        LOGV("revents for %d = 0x%08x", i, mFDs[i].revents);
        if(mFDs[i].revents & POLLIN) {
          res = read(mFDs[i].fd, &iev, sizeof(iev));
          if (res == sizeof(iev)) {
            LOGV("%s got: t0=%d, t1=%d, type=%d, code=%d, v=%d",
               mDevices[i]->path.string(),
               (int) iev.time.tv_sec, (int) iev.time.tv_usec,
               iev.type, iev.code, iev.value);
            *outDeviceId = mDevices[i]->id;
            if (*outDeviceId == mFirstKeyboardId) *outDeviceId = 0;
            *outType = iev.type;
            *outScancode = iev.code;
            if (iev.type == EV_KEY) {
              err = mDevices[i]->layoutMap->map(iev.code, outKeycode, outFlags);
              LOGV("iev.code=%d outKeycode=%d outFlags=0x%08x err=%d\n",
                iev.code, *outKeycode, *outFlags, err);
              if (err != 0) {
                *outKeycode = 0;
                *outFlags = 0;
              }
            } else {
              *outKeycode = iev.code;
            }
            *outValue = iev.value;
            *outWhen = s2ns(iev.time.tv_sec) + us2ns(iev.time.tv_usec);
            return true;
          } else {
            if (res<0) {
              LOGW("could not get event (errno=%d)", errno);
            } else {
              LOGE("could not get event (wrong size: %d)", res);
            }
            continue;
          }
        }
      }
    }
 ...
}

对于按键事件,调用mDevices[i]->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_readEvent,用于读取输入设备事件。

static jboolean
android_server_KeyInputQueue_readEvent(JNIEnv* env, jobject clazz, jobject event)
{
  gLock.lock();
  sp hub = gHub;
  if (hub == NULL) {
    hub = new EventHub;
    gHub = hub;
  }
  gLock.unlock();
  int32_t deviceId;
  int32_t type;
  int32_t scancode, keycode;
  uint32_t flags;
  int32_t value;
  nsecs_t when;
  bool res = hub->getEvent(&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);
  env->SetIntField(event, gInputOffsets.mFlags, (jint)flags);
  env->SetIntField(event, gInputOffsets.mValue, value);
  env->SetLongField(event, gInputOffsets.mWhen, (jlong)(nanoseconds_to_milliseconds(when)));
  return res;
}

readEvent调用hub->getEvent读了取事件,然后转换成JAVA的结构。

事件中转线程

在frameworks/base/services/java/com/android/server/KeyInputQueue.java里创建了一个线程,它循环的读取事件,然后把事件放入事件队列里。

Thread mThread = new Thread("InputDeviceReader") {
  public void run() {
      android.os.Process.setThreadPriority(
          android.os.Process.THREAD_PRIORITY_URGENT_DISPLAY);
        try {
        RawInputEvent ev = new RawInputEvent();
        while (true) {
          InputDevice di;
        readEvent(ev);
        send = preprocessEvent(di, ev);
          addLocked(di, curTime, ev.flags, ..., me);
        }
    }
  };
}

输入事件分发线程

在frameworks/base/services/java/com/android/server/WindowManagerService.java里创建了一个输入事件分发线程,它负责把事件分发到相应的窗口上去。

mQueue.getEvent
dispatchKey/dispatchPointer/dispatchTrackball

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Android设计登录界面、找回密码、注册功能

这篇文章主要为大家详细介绍了Android设计登录界面的方法,Android实现找回密码、注册功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android通过手势实现答题器翻页效果

这篇文章主要为大家详细介绍了Android通过手势实现答题器翻页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android采用双缓冲技术实现画板

这篇文章主要为大家详细介绍了Android采用双缓冲技术实现画板的相关资料,思路清晰,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发之毛玻璃效果实例代码

这篇文章主要给大家分享android开发之毛玻璃效果的实例代码,非常具有参考借鉴价值,感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

Android实现桌面悬浮窗、蒙板效果实例代码

这篇文章主要介绍了Android实现桌面悬浮窗、蒙板效果实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解读Android的Volley库的功能结构

这篇文章主要介绍了Android的Volley开发框架的功能结构,Volley是Android开发中网络部分的一大利器,包含很多HTTP协议通信的相关操作,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发中使用Volley库发送HTTP请求的实例教程

这篇文章主要介绍了Android开发中使用Volley库发送HTTP请求的实例教程,包括创建Volley单例的基本知识与取消Request请求的技巧等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿QQ聊天撒花特效 很真实

本文写的这个特效,是关于聊天的,你肯定遇到过,就是你跟人家聊天的时候,比如发送应(么么哒),然后屏幕上全部就是表情了,今天我们就是做这个,撒花的特效,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android的HTTP操作库Volley的基本使用教程

这篇文章主要介绍了Android的HTTP操作库Volley的基本使用教程,包括JSON请求与图片加载等用法的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿水波纹流量球进度条控制器

这篇文章主要介绍了Android仿水波纹流量球进度条控制器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多