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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…