-1

Now, I want to implement a function like this: at a activity, shield all hardware buttons, like HOME, VOLUME_UP,VOLUME_DOWM. But, only POWER button can't shield. Is there a way?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Big Bang
  • 19
  • 3
  • 1
    "Is there a way?" -- fortunately, no, for obvious security reasons. Users need to retain control over their device, and so being able to return to the home screen, reboot the device into safe mode, and so on is important. – CommonsWare Jul 04 '12 at 12:14
  • just out of curiosity in case your app gets hanged user has to take out the battery ? – Sunny Kumar Aditya Jul 04 '12 at 12:18

1 Answers1

1

You cannot intercept certain keys in Android like power and home e.g. Home key cannot be intercepted as this will allow any malicious application to over ride the functionality.

The only way you can intercept the home key is if your application is the home app replacement itself by using android.intent.category.HOME in the manifest.

You can catch other keys e.g. volume up/down and back as follows

public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (action == KeyEvent.ACTION_UP) {
            //handle the key event
        }
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN) {
            //handle the key event
        }
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}

Read the following post on the Android Developer blogpost

Android Developer: Back and other hard keys-three stories

Orlymee
  • 2,349
  • 1
  • 22
  • 24