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)开发者_如何学Go {
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_CALLDo you know any workaround to block two those buttons?
Is the only way (as often) - write in C ?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.
精彩评论