屏蔽home键、重写home键操作
2013-06-05 23:02 ...平..淡... 阅读(3052) 评论(0) 编辑 收藏 举报背景:
按下home键后,进入图库通过WifiDirect分享,此时获得的Intent为android.intent.action.MAIN,而按下back键进行操作,此时Intent是android.intent.action.SEND/android.intent.action.SEND_MUITIPLE
分析: back键和home键的区别
KeyEvent.KEYCODE_BACK (back键)
back键默认行为是finish处于前台的Activity,将该task从栈中弹出。即Activity的状态为Destroy状态为止,再次启动该Activity是从onCreate开始的(不会调用onSaveInstanceState方法)。 KeyEvent.KEYCODE_HOME (home键) Home键默认是stop前台的Activity即状态为onStop为止而不是Destroy,若再次启动它,会调用onSaveInstanceState方法,不过个人认为不排除因为内存而被回收的可能,这只能将这个task保存到栈中。保持上次Activity的状态则是从
OnRestart开始的---->onStart()--->onResume()。
|
---------------------------------------------------------------------------------------------------------------------------------------
网上有说要加个权限的,如下:
首先:加权限禁止Home键
<uses-permission android:name=”android.permission.DISABLE_KEYGUARD” />
大家不要以为添加这个权限后,所有的Activity的Home键都不可以用了,反而添加这个权限后,跟不添加是一样的效果,就是所有的Activity的Home键都没有禁止,如果你想对某个Activity禁止Home的事件,那么你只需要在某个Activity中加上此方法:
@Override public void onAttachedToWindow() { this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); }
这个时候,此Activity的Home键才被禁止掉,然后在onKeyDown方法中进行自己的操作。
ps:我试了下,不加权限<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>,也是可以实现的。
---------------------------------------------------------------------------------------------------------------------------------------
我的总结:
屏蔽、重写home键操作:
(1)屏蔽home键
(2)重写home键
该方法是重写home键操作
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(KeyEvent.KEYCODE_HOME == keyCode){ //判断是否为home键 finish(); //结束该activity return true; } return super.onKeyDown(keyCode, event); }
该方法是拦截、屏蔽home键
/** * 官方文档:Called when the main window associated with the activity has been attached to the window manager. * 即 当关联此activity的主窗口被附加到窗口管理类时,该方法被调用。实际上是在onResume方法执行后调用 */ @Override public void onAttachedToWindow() { //分析setType参数为TYPE_KEYGUARD的原因,见(a) this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); }
(a)/frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java
...... // First we always handle the home key here, so applications // can never break it, although if keyguard is on, we do let // it handle it, because that gives us the correct 5 second // timeout. 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) { //如果type是TYPE_KEYGUARD或TYPE_KEYGUARD_DIALOG,则获得key // 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; } } } } ......
continue my dream...