我正在尝试在documentation中描述的Glass应用程序中实现上下文语音命令。
我的布局中有一个FrameLayout,在我的活动中,我正在实现OnKeyDown以识别水龙头。但是,当用户说“Ok glass”然后选择其中一个可用命令时,会返回相同的活动,但没有响应(我甚至无法通过向下滑动关闭应用程序)。
我尝试以几种不同的方式在FrameLayout上使用requestFocus(),但它没有任何效果。如何在不重新启动活动的情况下重新获得焦点?
基本上,这是我到目前为止主要活动的代码:
public class MainActivity extends Activity {
public static String TAG = "DummyProject::MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
getMenuInflater().inflate(R.layout.main, menu);
return true;
}
return super.onCreatePanelMenu(featureId, menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.layout.main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
switch (item.getItemId()) {
case R.id.awesome:
Log.i(TAG, "Selected!");
break;
default:
return true;
}
return true;
}
return super.onMenuItemSelected(featureId, item);
}
@Override
public boolean onKeyDown(int keycode, KeyEvent event) {
if(keycode == KeyEvent.KEYCODE_DPAD_CENTER) {
Log.i(TAG, "tap!");
return true;
}
return super.onKeyDown(keycode, event);
}
}
对于此示例,在我选择任何“ok glass”菜单选项后,不会调用onKeyDown。
提前谢谢。
答案 0 :(得分:4)
更喜欢GestureDetector捕捉触摸板事件...
...
private GestureDetector mGestureDetector;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mGestureDetector = createGestureDetector(this);
...
}
...
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetector = new GestureDetector(context);
// Create a base listener for generic gestures
gestureDetector.setBaseListener(new GestureDetector.BaseListener() {
@Override
public boolean onGesture(Gesture gesture) {
if (gesture == Gesture.TAP) {
Log.d(TAG, "TAP!!!");
return true;
}
return false; }
});
gestureDetector.setFingerListener(new GestureDetector.FingerListener() {
@Override
public void onFingerCountChanged(int previousCount, int currentCount) {
// do something on finger count changes
}
});
gestureDetector.setScrollListener(new GestureDetector.ScrollListener() {
@Override
public boolean onScroll(float displacement, float delta, float velocity) {
// do something on scrolling
return true;
}
});
return gestureDetector;
}
/*
* Send generic motion events to the gesture detector
*/
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (mGestureDetector != null) {
return mGestureDetector.onMotionEvent(event);
}
return false;
}
答案 1 :(得分:0)
事实证明问题出在Manifest文件中。我正在为应用程序设置一个主题,即
<application android:theme="@android:style/Theme.NoTitleBar"> ...
一旦我删除了主题,一切都很完美。当我试图找到这个奇怪的错误时,谷歌的GDK demo sample很有帮助。