如何用
Android SDK抓住手机按键?我一直在寻找几个小时,没有找到任何东西..
例如:
在某些情况下,当用户按下手机上的“挂断”按钮时,我想抓住该信息,然后在该消息到达操作系统之前丢弃该消息.
这可能吗?
解决方法
您可以处理从整个应用程序的视图或一般的关键事件:
从视图处理钥匙:
public boolean onKey(View v,int keyCode,KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true;
}
return false;
}
记住实现OnKeyListener并设置你的监听器YourView.setonKeyListener(this);
第二种可能性是:
@Override
public boolean onKeyDown(int keyCode,KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
/* Sample for handling the Menu button globally */
return true;
}
return false;
}
你也可以看看onKeyUp.
资源:http://developer.android.com/reference/android/view/View.html
在这里,您可以看到所有KeyEvents的列表