dispatchKeyEvent和onKeyDown
来源:互联网 发布:java的引用类型 编辑:程序博客网 时间:2024/06/07 23:50
转载:http://blog.csdn.net/tanqiantot/article/details/8060015
1)当我们重写了onKeyDown方法后,如果return false,则会继续调用系统的onKeyDown方法。
如果只想让程序调用自己写的onKeyDown,则需要return true。
利用该特性可以拦截耳机耳机按键,防止启动音乐。
下面转载一下拦截屏幕按键的方法:
在Android系统中用来显示界面的组件(Component)为Activity,也就是说只有重写Activity的onKeyDown方法来监控/拦截/屏蔽系统的返回键(back)、菜单键(Menu)及Home键。
1、拦截/屏蔽返回键、菜单键实现代码
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
@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键
1
2
3
4
2
3
4
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
为什么必须重写onAttachedToWindow(),看看下面的代码就知道了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/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方法比较好。
dispatchKeyEvent和onKeyDown
return true
0 0
- dispatchKeyEvent和onKeyDown
- dispatchKeyEvent和onKeyDown
- dispatchKeyEvent和onKeyDown
- dispatchKeyEvent和onKeyDown
- 关于dispatchKeyEvent和onKeyDown关系
- dispatchKeyEvent onKeyDown
- Android dispatchKeyEvent和onKeyDown事件的处理
- Android提供的onKey(),OnKeyDown()和dispatchKeyEvent()方法之间的区别
- onkeyup,onkeydown和onkeypress
- onkeyup,onkeydown和onkeypress
- onkeyup,onkeydown和onkeypress
- onKeyDown和onBackPressed
- onkeyup,onkeydown和onkeypress
- onKeyDown()
- onKeyDown
- onKeyDown
- onkeydown
- 转 : onkeyup,onkeydown和onkeypress
- HDU3371——Connect the Cities
- but addTarget: action:
- 真的觉得好累了
- Apache Shiro 使用手册(一)Shiro架构介绍
- 黑马程序员----[20150506][集合框架]
- dispatchKeyEvent和onKeyDown
- ipbegin103.111.112.132ipend
- jqplot柱状图上数值显示
- TDSQL在微众银行核心交易系统中的实践
- hadoop运行程序出现Error: Java heap space
- 基于FFmpeg实现MP3自组织seek策略偶发解码后Resample出错问题原理释义
- 几个iOS工程通用模块介绍
- 创建型模式
- 杭电ACM 2027:统计元音