如何在Android App中屏蔽Home按键及其他按键
在Android App中屏蔽Home按键及其他按键,需要在APP中添加以下代码:
屏蔽HOME按键,在onAttachedToWindow中设置FLAG_HOMEKEY_DISPATCHED
屏蔽除Power , Home外的按键,覆盖dispatchKeyEvent方法,并返回为true
public class LockByKeyGuard extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onAttachedToWindow() {
//关键:在onAttachedToWindow中设置FLAG_HOMEKEY_DISPATCHED
this.getWindow().addFlags(WindowManager.LayoutParams. FLAG_HOMEKEY_DISPATCHED);
super.onAttachedToWindow();
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
// 返回true,不响应其他key
return true;
}