views:

1608

answers:

2

Hi!

If you ever tried to write a locker app on Android sure you meet this problem:

boolean mBackPressed = false;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            mBackPressed = true;
            break;
        case KeyEvent.KEYCODE_MENU:
            if (mBackPressed)
                unLock();
            break;
        default:
            mBackPressed = false;
            showMessage();
            break;
        }
    }
    return true;
}

private void showMessage() {
    Toast.makeText(getBaseContext(), "Back + Menu", Toast.LENGTH_SHORT)
            .show();
}

private void unLock() {
    this.setResult(Activity.RESULT_OK);
    this.finish();
}

Seems like onKeyDown is filtering out all keys but "Back" and "Menu"...
Well, it's not true! Home button will still bring you Home screen and End Call button will run native Locker application!

Fellow's out there also claim it as a problem:
How to listen from ENDCALL button
problem With Home Back screen button
Supressing Key presses in Activity, especially in Options Menu
Issue 4202: Feature Suggestion: permission for intercepting KEYCODE_CALL

Do you know any workaround to block two those buttons?
Is the only way (as often) - write in C ?

+4  A: 

You can capture the Back key quite easily.

I don't think you'll be able to intercept the Home and End Call buttons. If you could, this would allow a malicious application to prevent a user ever leaving it, effectively hijacking the phone.

An option for your application would be to write a replacement Home Screen using the android.intent.category.HOME Intent.

Dave Webb
Ugh... I've misplaced a couple buttons there... sorry for that. it's the Home and EndCall I need to handle.
Max Gontar
Talking about malicious application, I believe user can alway reset phone or something like that. Sure it's a trouble but only once per malicious.
Max Gontar
I think it's by design that you can't capture Home and EndCall so the user can always leave an application the same way. One option for you might be to write a replacement Home Screen using the `android.intent.category.HOME` Intent.
Dave Webb
+1 Thanks for suggestions!
Max Gontar
A: 

hi guys... evn i am struggling from this problem,,, can u pls give me a sure shot solution of this prb , as this is an immediate issue for me.......

jhonybravo