tags:

views:

132

answers:

4

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 ?

+1  A: 

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 Chandrasekaran
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
psicho
+1  A: 

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

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

The above however will only intercept the back button and block it

As for creating a dialog that cannot be removed you should implement a custom dialog. Make sure not to add any buttons or call dimiss/cancel.

new AlertDialog.Builder(this)
      .setMessage("Message"))
      .setOnCancelListener(new OnCancelListener() {
        public void onCancel(DialogInterface dialog) {
          return false;
        }});
      .show();
}  
BeRecursive
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
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
Thanks a lot, im going to try it
psicho
A: 

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

psicho
+1  A: 

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