android4.0 中如何在app 中屏蔽HOME键
来源:互联网 发布:打淘宝电话怎么转人工 编辑:程序博客网 时间:2024/05/21 11:33
新发现的拦截home 建的办法
/** 拦截HOME/MENU */
sendBroadcast((new Intent("android.intent.action.AgamaECGEnter")));
/** 取消拦截HOME/MENU */
sendBroadcast((new Intent("android.intent.action.AgamaECGLeave")));
以下为转载
近日在研究一个视频锁的功能,即在 视频播放界面上设一个锁的功能,当该锁起效后,就会屏蔽back, home, menu三个键的功能。
back 和 menu 键都可以通过 app层 的 onKeyDown 和 onKeyUp 函数来拦截。但是 home 键不行。
于是上网查了一下在app层屏蔽 home 键的方法。
主流的方法如下所示。但只在2.2,2.3平台下有效。(下面列举的方法只在android 2.2, 2.3 上有效)
屏蔽Activity, Dialog风格Activity, AlertDialog的Home键功能方法分别是:
1.屏蔽Activity中的Home键功能,只需要在你要屏蔽的activity 中重写 onAttachToWindow() 这个函数就可以了
[java]
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
2.屏蔽 Dialog风格Activity的Home键功能,也需要在你要屏蔽的activity 中重写 onAttachToWindow() 这个函数,但设置window 的值需要改变为 TYPE_KEYGUARD_DIALOG
[java]
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
这里之所以和Activity区分,是因为Dialog风格Activity如果用第一种方法,这个Dialog风格Activity的背景就变成黑色的,而不是透明的.
3.屏蔽AlertDialog的Home键功能
[java]
AlertDialog d = b.create();
d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
AlertDialog d = b.create();
d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
后面通过上面一篇文章的思路来看 4.0平台的 framework 层的代码,发现在 PhoneWindowManager.java 的 interceptKeyBeforeDispatching 函数中对home键的处理如下:
[java]
if (keyCode == KeyEvent.KEYCODE_HOME) {
// If we have released the home key, and didn't do anything else
// while it was pressed, then it is time to go home!
if (mHomePressed && !down) {
mHomePressed = false;
..
}
// 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 0;
}
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 -1;
}
}
final int flag = attrs.flags;
if ((flag & WindowManager.LayoutParams.FLAG_HOMEKEY_DISPATCHED) != 0) {
// the window wants to handle the home key, so dispatch it to it.
return 0;
}
}
return -1;
if (keyCode == KeyEvent.KEYCODE_HOME) {
// If we have released the home key, and didn't do anything else
// while it was pressed, then it is time to go home!
if (mHomePressed && !down) {
mHomePressed = false;
......
}
// 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 0;
}
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 -1;
}
}
final int flag = attrs.flags;
if ((flag & WindowManager.LayoutParams.FLAG_HOMEKEY_DISPATCHED) != 0) {
// the window wants to handle the home key, so dispatch it to it.
return 0;
}
}
return -1;
}
在此函数中,如果返回 0,则 home 键会交给 app 来处理,如果返回 -1 ,则不会处理 home 键。
在app 的你要屏蔽home 键的activity 中,只需要添加该标志就可以了
getWindow().addFlags(WindowManager.LayoutParams.FLAG_HOMEKEY_DISPATCHED);
如果要使home 键有效,再 clearFlags 即可。
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_HOMEKEY_DISPATCHED);
从上面的代码来看,应该设置 window 的类型也是有效的,但不知为何在 4.0 中不取作用,打Log 看也不取效。不知何故。
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
作者:fulinwsuafcie
- android4.0 中如何在app 中屏蔽HOME键
- android4.0 中如何在app 中拦截HOME键
- android4.0 中如何在app 中拦截HOME键
- android4.4中屏蔽home键
- 安卓4.0 中如何在app 中屏蔽HOME键(包含2.2 2.3)
- 安卓4.0 中如何在app 中屏蔽HOME键
- android4.0屏蔽home键
- android4.0 屏蔽home键
- 如何在Android App中屏蔽(拦截)Home按键及其他按键
- 如何在Android App中屏蔽(拦截)Home按键及其他按键
- 如何在Android App中屏蔽(拦截)Home按键及其他按键
- android4.0屏蔽HOME键的方法
- android4.2 屏蔽home键
- android4.2 屏蔽home键
- Android 中屏蔽 Home 键
- Android 中屏蔽 Home 键
- 在activity中屏蔽home键的方法
- 如何屏蔽Home键
- Android开发环境部署方法
- 论下一代在线安全审计软件
- OpenGL ES着色器语言之着色语言语法(官方文档第九章)
- 一步一步复习数据结构和算法基础-顺序查找+二分查找
- UVA12461 Airplane 水题一枚
- android4.0 中如何在app 中屏蔽HOME键
- 泛型集合工具类
- Android oprofile详细分析应用程序、动态库、内核函数调用时间
- JAVA内存溢出解析
- ArcGIS 10.1 for Server 在windows下的安装
- Adobe read X安装过程中出现无法将数值DisableExceptionChainValidation写入键
- WaitForSingleObject
- [Economist] Heartless attacks 无情的攻击
- CX51 用户手册----REGISTERBANK伪指令