11

How can i catch a phone keypress with the android SDK? i've been looking around for hours without finding anything..

For example:

In some cases, i want to catch the message when a user presses the "hang up" button on the phone, and then discard the message before it reaches the OS.

Is this possible?

4

1 回答 1

19

您可以从视图或一般为整个应用程序处理关键事件:

从视图处理 onKey:

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的列表

于 2010-02-14T17:34:43.037 回答