Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

key - Android - capture/suppress Home and EndCall buttons events?

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...