问题
I've successfully populated the contextual voice menu of my glassware immersion but I want to be able to change the menu after the initial set up in onPreparePanel() or onCreatePanelMenu() as described here: Programmatically populated contextual "ok glass" menu.
Ideally one of the callbacks would be called after receiving the 'ok, glass' voice command but as far as I can tell, none of them are. Outcome, you're stuck with the version of the voice commands menu as it was the first time you cam into the activity.
So far, the only hack that's come close was when I pulled the GlassVoice.apk off the device and set up a manual VoiceListener (as discussed here: Glass voice command nearest match from given list). When the voice command is detected an onVoiceCommand is called, I try to force a refresh of the menu by calling invalidateOptionsMenu() (no effect) or getWindow().invalidatePanelMenu(WindowUtils.FEATURE_VOICE_COMMANDS). This second one looks like it tries to pull up the menu but then instantly hides it again.
How can I dynamically change the contextual voice menu later on in the lifetime of the activity?
回答1:
I actually just wrote a sample app and this is working fine for me.
Be sure to use onCreatePanelMenu()
to create the voice menu and check
if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS ||
featureId == Window.FEATURE_OPTIONS_PANEL) ...
to inflate the menu for both touch menu and voice menu.
To refresh both, in onMenuItemSelected()
, make sure to call both invalidateOptionsMenu()
to refresh the touch menu and getWindow().invalidatePanelMenu(WindowUtils.FEATURE_VOICE_COMMANDS)
to refresh the voice menu. I just flipped a boolean on the first menu item selection, which I used to determine whether an old or new menu should be inflated.
What version of Glass are you running on?
来源:https://stackoverflow.com/questions/24762428/how-do-you-customise-glass-contextual-voice-menu-in-an-immersion-after-its-ini