『壹』 如何在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;
}