最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程輸入事件流程詳解

 更新時間:2016年10月26日 10:11:39   作者:Wallace  
這篇文章主要介紹了Android編程輸入事件流程,較為詳細的分析了Android輸入事件原理、相關(guān)概念與具體操作流程,需要的朋友可以參考下

本文實例講述了Android編程輸入事件流程。分享給大家供大家參考,具體如下:

EventHub對輸入設(shè)備進行了封裝。輸入設(shè)備驅(qū)動程序?qū)τ脩艨臻g應(yīng)用程序提供一些設(shè)備文件,這些設(shè)備文件放在/dev/input里面。

EventHub掃描/dev/input下所有設(shè)備文件,并打開它們。

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對外提供了一個函數(shù)用于從輸入設(shè)備文件中讀取數(shù)據(jù)。

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;
          }
        }
      }
    }
 ...
}

對于按鍵事件,調(diào)用mDevices[i]->layoutMap->map進行映射。映射實際是由 KeyLayoutMap::map完成的,KeyLayoutMap類里讀取配置文件qwerty.kl,由配置文件qwerty.kl決定鍵值的映射關(guān)系。你可以通過修改./development/emulator/keymaps/qwerty.kl來改變鍵值的映射關(guān)系。

JNI函數(shù)

在frameworks/base/services/jni/com_android_server_KeyInputQueue.cpp文件中,向JAVA提供了函數(shù)android_server_KeyInputQueue_readEvent,用于讀取輸入設(shè)備事件。

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調(diào)用hub->getEvent讀了取事件,然后轉(zhuǎn)換成JAVA的結(jié)構(gòu)。

事件中轉(zhuǎn)線程

在frameworks/base/services/java/com/android/server/KeyInputQueue.java里創(chuàng)建了一個線程,它循環(huán)的讀取事件,然后把事件放入事件隊列里。

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);
        }
    }
  };
}

輸入事件分發(fā)線程

在frameworks/base/services/java/com/android/server/WindowManagerService.java里創(chuàng)建了一個輸入事件分發(fā)線程,它負責(zé)把事件分發(fā)到相應(yīng)的窗口上去。

mQueue.getEvent
dispatchKey/dispatchPointer/dispatchTrackball

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

最新評論

广安市| 大关县| 盐津县| 蒙城县| 赞皇县| 乌拉特前旗| 垦利县| 宁津县| 四子王旗| 岚皋县| 逊克县| 安图县| 家居| 揭西县| 辉县市| 子洲县| 宜兴市| 清河县| 灵璧县| 遂川县| 大冶市| 连州市| 河东区| 栖霞市| 肥西县| 沽源县| 江山市| 永城市| 临沧市| 寿宁县| 泾源县| 佛学| 新丰县| 马山县| 吉木乃县| 凤山市| 永新县| 黎平县| 卢氏县| 兴宁市| 岑巩县|