2014-10-12 39 views
0

遵循Google自己提供的教程後,仍然只識別語音命令。支持Google Glass中的語音和觸控命令

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已經改變,他們還沒有更新他們的帖子?

回答

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; 
} 
+1

我只是想你的建議到底是什麼,但仍然好像沒有什麼改變。然而,使用onClick方法確實有效。 在附註上:Eclipse顯示編譯器警告,指出從未使用在最開始聲明的「mGestureDetector」。 想法? 編輯:刪除的代碼,因爲我無法讓它格式正確 – schlingel 2014-10-13 18:51:39

+1

add onGenericMotionEvent。我忘了它 – pt2121 2014-10-13 19:42:56

+2

這使它工作,謝謝你! – schlingel 2014-10-14 16:19:38

相關問題