android 屏蔽home键操作
2012-03-06 20:35
344 查看
1、重写onAttachedToWindow
2、 重写onKeyDown
3 android源码
因为android系统自己对home键在PhoneWindowManager中做了处理,不会返回到上层应用。查看源代码:
\frameworks\policies\base\phone\com\android\internal\policy\impl\PhoneWindowManager.java 1089行
4 Dialog
如果在Activity弹出dialog,在Activity设置以上2个方法是没办法屏蔽的。
其实,原理是一样的,只是地方不一样而已。
参考:
dialog,activity 屏蔽Home键详解
WindowManager.LayoutParams
原文:http://www.cnblogs.com/myparamita/archive/2011/10/28/2228054.html
public void onAttachedToWindow() { this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); }
2、 重写onKeyDown
public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KeyEvent.KEYCODE_HOME: Log.i(TAG,"KEYCODE_HOME"); return true; } return super.onKeyDown(keyCode, event); }
3 android源码
因为android系统自己对home键在PhoneWindowManager中做了处理,不会返回到上层应用。查看源代码:
\frameworks\policies\base\phone\com\android\internal\policy\impl\PhoneWindowManager.java 1089行
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; } } }
4 Dialog
如果在Activity弹出dialog,在Activity设置以上2个方法是没办法屏蔽的。
其实,原理是一样的,只是地方不一样而已。
final Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.mydailog); dialog.show(); //show应在setType之前,否则报错 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); dialog.setOnKeyListener(new android.content.DialogInterface.OnKeyListener(){ @Override public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_HOME: Log.i(TAG,"KEYCODE_HOME"); return true; } return false; } });
参考:
dialog,activity 屏蔽Home键详解
WindowManager.LayoutParams
原文:http://www.cnblogs.com/myparamita/archive/2011/10/28/2228054.html
相关文章推荐
- android 屏蔽home键操作
- android屏蔽返回键和home键等各个按键的方法
- android Home键 屏蔽,捕获,修改
- android屏蔽返回键,home键等实体…
- Android dialog,activity 屏蔽Home键的教程详解
- android 采用windowmanager屏蔽HOME键
- Android 4.4.源码 如何屏蔽Home键
- android 屏蔽home键
- android home键屏蔽
- 关于Android App上线后从应用市场点击打开按钮进入应用后进行操作后,再点击home键到桌面后点击启动图标重新打开应用的解决方法
- Android笔记--实现Android4.1.2对home键的屏蔽
- android屏蔽返回键和Home键
- 另辟思路解决Android 4.0以后不能监听和屏蔽Home键的问题
- android 關於屏蔽Home键的問題
- Android开发笔记--屏蔽返回键,home键以及其他实体按键
- Android屏蔽设备的HOME键
- Android实现对HOME键的捕获和屏蔽
- android如何屏蔽掉home键
- Android编程实现Home键的屏蔽,捕获与修改方法
- Android 屏蔽BACK键和HOME键的方法