通过正常的onKeyDown方法,无法屏蔽Home键。
在SDK4.0版本下可以这样:
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
拦截Home键:
忘记在哪儿看到的了,原理大概是:在Home退出时,系统会关掉一个Dialog,我们监听那个Dialog的消失广播就可以了。
不过,接收广播也需要时间,在当前Activity调用OnPause()后。
registerReceiver(homeKeyEventBroadCastReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
private BroadcastReceiver homeKeyEventBroadCastReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra("reason");
if (reason != null) {
if (reason.equals("homekey")) {
// home key处理点
} else if (reason.equals("recentapps")) {
// long home key处理点
}
}
}
}
};