Programmatically populated contextual “ok glass” menu

一世执手 提交于 2019-12-06 11:32:17

问题


Is there any way to populate the custom "ok glass" menu in my glassware programmatically?

I have an application where the user will be in an immersion and interact with the system mainly by voice commands. The immersion consists of a CardScrollView displaying different sets of data. These sets are added and removed dynamically from a bluetooth service talking to a phone and the glass unit can't know in advance what new sets will appear.

What I want the user to be able to do is to list all current sets in the voice menu and from there choose which set to switch to. For example, if I at the moment have the sets A, B, C and D, I want the user to be able to say "ok glass, go to set", see a sub menu with A, B, C and D and then say for example "C" to switch to set C in the view.

Is this at all possible?

The glassware is going to run in a closed environment with no connection to MyGlass at all, so custom voice commands for the menu with the development permission is not a problem.


回答1:


From what I understand you want you application to be already running when the user speaks. If this is correct then you can simply implement a custom menu with contextual voice commands. I believe you can always repopulate the menu just before it is being shown by overriding onPreparePanel.

I haven't tested it but guessing from the guide something like:

  @Override
  public boolean onPreparePanel(int featureId, View view, Menu menu) {
    if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
      menu.clear();
      for (MyMenuItem item : mCurrentMenuItems) {
        menu.add(Menu.NONE, item.getId(), Menu.NONE, item.getTitle());
      }
    }
    return super.onPreparePanel(featureId, view, menu);
  }

  @Override
  public boolean onMenuItemSelected(int featureId, MenuItem item) {
    if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
      switch (item.getItemId()) {
        case MENU_ITEM_A:
          // do something
          break;
        default:
          return true;
      }
      return true;
    }
    return super.onMenuItemSelected(featureId, item);
  }

MyMenuItem would be a simple class which holds a unique id of an item and its title. mCurrentMenuItems is a list of items to be shown at the moment. You can change its content using a background service, for example.



来源:https://stackoverflow.com/questions/24386276/programmatically-populated-contextual-ok-glass-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!