Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home键
来源:互联网 发布:浪潮软件利好消息 编辑:程序博客网 时间:2024/05/21 17:09
在Android系统中用来显示界面的组件(Component)为Activity,也就是说只有重写Activity的onKeyDown方法来监控/拦截/屏蔽系统的返回键(back)、菜单键(Menu)及Home键。
1、拦截/屏蔽返回键、菜单键实现代码
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键
processExit();
return true;
} else if(keyCode == KeyEvent.KEYCODE_MENU) {
//监控/拦截菜单键
} else if(keyCode == KeyEvent.KEYCODE_HOME) {
//由于Home键为系统键,此处不能捕获,需要重写onAttachedToWindow()
}
return super.onKeyDown(keyCode, event);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键
processExit();
return true;
} else if(keyCode == KeyEvent.KEYCODE_MENU) {
//监控/拦截菜单键
} else if(keyCode == KeyEvent.KEYCODE_HOME) {
//由于Home键为系统键,此处不能捕获,需要重写onAttachedToWindow()
}
return super.onKeyDown(keyCode, event);
}
2、拦截/屏蔽系统Home键
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
为什么必须重写onAttachedToWindow(),看看下面的代码就知道了
/frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java 1000行附近
if (code == KeyEvent.KEYCODE_HOME) {
// If a system window has focus, then it doesn't make sense
// right now to interact with applications.
WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
if (attrs != null) {
final int type = attrs.type;
if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
|| type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
// the "app" is keyguard, so give it the key
return false;
}
final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
for (int i=0; i<typeCount; i++) {
if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {
// don't do anything, but also don't pass it to the app
return true;
}
}
}
}
if (code == KeyEvent.KEYCODE_HOME) {
// If a system window has focus, then it doesn't make sense
// right now to interact with applications.
WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
if (attrs != null) {
final int type = attrs.type;
if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
|| type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
// the "app" is keyguard, so give it the key
return false;
}
final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
for (int i=0; i<typeCount; i++) {
if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {
// don't do anything, but also don't pass it to the app
return true;
}
}
}
}
当然,重写View的onKeyDown也可以实现,但View如果没有获得焦点,那就不能监控/拦截返回键、菜单键和Home键,所有还是重写Activity的onKeyDown方法比较好。
0 0
- Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home键
- Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home键
- Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home键
- Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home键
- Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home键
- Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home键
- Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home键
- Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home键
- Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home
- 重写onKeyDown方法,监控返回键、菜单键和Home键
- Android 屏蔽返回键、菜单键和Home键
- Android 屏蔽返回键、菜单键和Home键
- 监听和拦截android系统返回键
- ANDROID 返回,菜单和HOME键的监听
- ANDROID 返回,菜单和HOME键的监听
- ANDROID 返回,菜单和HOME键的监听
- android屏蔽返回键和Home键
- Android屏蔽返回键和Home键
- 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程(精)
- 快速排序
- javascript常用加入收藏与设为首页代码
- MFC内部运行来龙去脉追踪
- shell编程空格的使用
- Android系统onKeyDown监控/拦截/监听/屏蔽返回键、菜单键和Home键
- javascript node element
- SQL2008 数据库根据事物日志的还原
- 为什么我写的文章没了?
- HDU 4970 Killing Monsters 离线 差分
- Codeforces Round #262 (Div. 2) B
- 如何解决Xcode5上传应用时找不到图标的问题。
- 用KMP来求字符串的循环节
- 校验中文长度和英文长度