1

I would like to know how could i block the dial, home , back and the end call button on an android device.

I know this is possible because there is an application : TheftAware which does block all the buttons so they have no effect at all.

And I also would like to know how to make a dialog window or any kind of window which would stay on top no matter what (this is also done in theftaware).

They are also able to block(hide) the call screen... does someone know how are they doing that ?

Note: Does all this means that android is not that secure after all ?

Marek Szanyi
  • 2,348
  • 2
  • 22
  • 28
  • What device are you using? Are the dial and end call buttons actual hardware buttons or do you mean the icons on the homescreen and in the call dialog? – Janusz Sep 23 '10 at 12:08
  • I have tested it on many android devices and it was working on everyone , and yes I'm talking about the real hardware buttons on the device – Marek Szanyi Sep 23 '10 at 12:18

4 Answers4

2

I just wanted to clear up a few bits of information here.

The code example from BeRecursive is incorrect in a few ways. As already noted, it won't block the Home button, but it has other problems:

  1. In order to consume the event so the rest of the Android framework won't act upon it, you need to return true from the onKeyDown handler, not false. The contract is that true means the application handled the event and the framework should not perform the default key event handling. (Praveen's code example also has the same issue).

  2. Starting from Android 1.5 and later, the Android framework moved the action activation from onKeyDown to onKeyUp. So you'll also need to implement the blocking in the onKeyUp handler, not just the onKeyDown handler.

  3. It is possible to block the KeyEvent.KEYCODE_CALL button using this technique, but not the KeyEvent.KEYCODE_ENDCALL button. This appears to be for security reasons.

Finally, the trick of setting WindowManager.LayoutParams.TYPE_SYSTEM_ALERT didn't have any effect for me in terms of actually blocking any of the hardware buttons. It might be useful for supressing popups from other applications, but I haven't explored this fully.

There's lots of good information from the Android team in this blog post.

mportuesisf
  • 5,587
  • 2
  • 33
  • 26
1

you have override yourOnKeyDown() method for the Activity and It should return false for example for back Key i mention the example code. Check the List of KeyEvent here

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
Praveen
  • 90,477
  • 74
  • 177
  • 219
  • Nice, thanks a lot but this seems to work only with the Back button :) ..it does not block the home or call button for example – Marek Szanyi Sep 23 '10 at 11:37
1

You need to implement the onKeyDown and onKeyUp method in your activity and return true for each of your required buttons:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch(keyCode) {
        case KEYCODE_BACK:
        case KEYCODE_CALL:
            return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch(keyCode) {
        case KEYCODE_BACK:
        case KEYCODE_CALL:
            return true;
    }
    return super.onKeyUp(keyCode, event);
}

The above however will only intercept the back button and block it. Please note that for security reasons you are not able to block certain hardware buttons. This includes ENDCALL and HOME. The way the application TheftAware does this is likely to be by leveraging some unintended functionality in the NDK.

BeRecursive
  • 6,286
  • 1
  • 24
  • 41
  • You cannot block the HOME key via `onKeyDown()`, and you might not be able to block the CALL/ENDCALL buttons (not sure about the rules there). – CommonsWare Sep 23 '10 at 11:35
  • 2
    You could set the application as a home intent to replace the home screen. This would override the home button to open the application. I did not test this so thanks for the correction @CommonsWare – BeRecursive Sep 23 '10 at 11:56
  • Thanks a lot, im going to try it – Marek Szanyi Sep 23 '10 at 12:22
0

Okey So if you want to block the home button or the rest of the mentioned buttons you have to set your window as

WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

and set permission : android.permission.SYSTEM_ALERT_WINDOW

of course you have to override the onKeyDown as Praveen Chandrasekaran first mentioned

Community
  • 1
  • 1
Marek Szanyi
  • 2,348
  • 2
  • 22
  • 28