How to Block the Dial, Home, Back and End Call button on Android
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 sta开发者_JAVA技巧y 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 ?
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:
In order to consume the event so the rest of the Android framework won't act upon it, you need to return
true
from theonKeyDown
handler, notfalse
. The contract is thattrue
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).Starting from Android 1.5 and later, the Android framework moved the action activation from
onKeyDown
toonKeyUp
. So you'll also need to implement the blocking in theonKeyUp
handler, not just theonKeyDown
handler.It is possible to block the
KeyEvent.KEYCODE_CALL
button using this technique, but not theKeyEvent.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.
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);
}
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.
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
精彩评论