支持Google Glass中的语音和触摸命令

时间:2014-10-12 13:20:52

标签: android google-glass google-gdk

按照Google本身提供的教程后,仍然只能识别语音命令。

https://developers.google.com/glass/develop/gdk/voice?hl=de#voice-and-touch

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View testView = new CardBuilder(this, CardBuilder.Layout.TEXT)
        .setText("test123")
        .getView();     

    getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);
    getWindow().requestFeature(Window.FEATURE_OPTIONS_PANEL);
    setContentView(testView);   
}

@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
    if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS ||
            featureId == Window.FEATURE_OPTIONS_PANEL) {
        getMenuInflater().inflate(R.menu.menu_one, menu);
        return true;
    }
    // Pass through to super to setup touch menu.
    return super.onCreatePanelMenu(featureId, menu);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS || 
            featureId == Window.FEATURE_OPTIONS_PANEL) {
        switch (item.getItemId()) {
            case R.id.menu_one_item:
                //Some stuff
                //...
                break;
            default:
                return true;
        }
        return true;
    }
    return super.onMenuItemSelected(featureId, item);
}   

仅仅检查FEATURE_OPTIONS_PANEL还是有必要添加某种onClick侦听器真的够了吗?也许只是我无法按照预期的方式理解指令。

本教程是从7月31日开始的,自那时起API是否有可能发生变化,而且他们还没有更新帖子?

1 个答案:

答案 0 :(得分:4)

是的,您需要以某种方式检测点击,以便当用户点按时,您的应用会显示菜单。

示例:

private GestureDetector mGestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGestureDetector = createGestureDetector(this);
}

private GestureDetector createGestureDetector(Context context) {
    GestureDetector gestureDetector = new GestureDetector(context);

    gestureDetector.setBaseListener(new GestureDetector.BaseListener() {
        @Override
        public boolean onGesture(Gesture gesture) {
            // if the user taps, play a proper sound and open the menu.
            // you might don't need the || gesture == Gesture.LONG_PRESS
            if (gesture == Gesture.TAP || gesture == Gesture.LONG_PRESS) {
                AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                audio.playSoundEffect(Sounds.TAP);
                openOptionsMenu();
                return true;
            } 
            return false;
        }
    });
    return gestureDetector;
}

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    if (mGestureDetector != null) {
        return mGestureDetector.onMotionEvent(event);
    }
    return false;
}