android 4.0 屏蔽 HOME_KEY 和 RECENT_APP_KEY
2013-08-26 10:40
344 查看
在项目中需要屏蔽虚拟按键,back 和menu键可以屏蔽,但是HOME_KEY 和 RECENT_APP_KEY 却无法屏蔽,在 onKeyDown(int keyCode, KeyEvent event) 方法中不能捕获HOME_KEY 和 RECENT_APP_KEY 的动作。
1、屏蔽HOME_KEY
参考网站http://www.2cto.com/kf/201207/138886.html
在/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java(拦截消息的处理类)
的interceptKeyBeforeDispatching() 方法中做修改
在应用中设置以下代码
并在onKeyDown(int keyCode, KeyEvent event) 中返回true。屏蔽了HOME键。
2、屏蔽RECENT_APP_KEY
在PhoneWindowManager.java(拦截消息的处理类) 的interceptKeyBeforeDispatching() 方法中,RECENT_APP_KEY的事件不能截取。屏蔽RECENT_APP_KEY只能另想办法。
参考http://blog.csdn.net/yihongyuelan/article/details/7623578
在/frameworks/base/packages/SystemUI 中的TabletStatusBar.java (平板的StatusBar)。发现在类中存在一个广播类,添加一个屏蔽的动作和一个取消屏蔽的动作。达到通过广播去控制recentApp的动作是否响应。
最后在应用中添加以下代码就可以屏蔽了RECNET_APP_KEY
1、屏蔽HOME_KEY
参考网站http://www.2cto.com/kf/201207/138886.html
在/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java(拦截消息的处理类)
的interceptKeyBeforeDispatching() 方法中做修改
public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;
// 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; } } //以下条件屏蔽HOMEKEY final int flag = attrs.flags; if ((flag & FLAG_HOMEKEY_DISPATCHED) != 0){ Log.i(TAG, "============ FLAG_HOMEKEY_DISPATCHED"); return 0; } }
在应用中设置以下代码
public static final int FLAG_HOMEKEY_DISPATCHED = 0x80000000;public void onAttachedToWindow() {
getWindow().addFlags(FLAG_HOMEKEY_DISPATCHED);
super.onAttachedToWindow();
}
并在onKeyDown(int keyCode, KeyEvent event) 中返回true。屏蔽了HOME键。
2、屏蔽RECENT_APP_KEY
在PhoneWindowManager.java(拦截消息的处理类) 的interceptKeyBeforeDispatching() 方法中,RECENT_APP_KEY的事件不能截取。屏蔽RECENT_APP_KEY只能另想办法。
参考http://blog.csdn.net/yihongyuelan/article/details/7623578
在/frameworks/base/packages/SystemUI 中的TabletStatusBar.java (平板的StatusBar)。发现在类中存在一个广播类,添加一个屏蔽的动作和一个取消屏蔽的动作。达到通过广播去控制recentApp的动作是否响应。
protected View makeStatusBarView() { ... ... // receive broadcasts IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); filter.addAction(Intent.ACTION_SCREEN_OFF); //增加开关动作 filter.addAction(ACTION_DISABLE_RECENT_BUTTON); filter.addAction(ACTION_ENABLE_RECENT_BUTTON); context.registerReceiver(mBroadcastReceiver, filter); return sb; } private boolean disableRecentButton = false; private static final String ACTION_DISABLE_RECENT_BUTTON = "action_disable_recent_button"; private static final String ACTION_ENABLE_RECENT_BUTTON = "action_enable_recent_button"; private View.OnClickListener mOnClickListener = new View.OnClickListener() { public void onClick(View v) { if (v == mRecentButton) { if (!disableRecentButton) { //recentApp 动作处理 onClickRecentButton(); } } else if (v == mInputMethodSwitchButton) { onClickInputMethodSwitchButton(); } else if (v == mCompatModeButton) { onClickCompatModeButton(); } } }; private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action) || Intent.ACTION_SCREEN_OFF.equals(action)) { boolean excludeRecents = false; if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) { String reason = intent.getStringExtra("reason"); if (reason != null) { excludeRecents = reason.equals("recentapps"); } } if (Intent.ACTION_SCREEN_OFF.equals(action)) { // If we're turning the screen off, we want to hide the // recents panel with no animation // TODO: hide other things, like the notification tray, // with no animation as well mRecentsPanel.show(false, false); excludeRecents = true; } animateCollapse(excludeRecents); } //增加开关动作 else if (ACTION_DISABLE_RECENT_BUTTON.equals(action)){ disableRecentButton = true; } else if (ACTION_ENABLE_RECENT_BUTTON.equals(action)){ disableRecentButton = false; } } };
最后在应用中添加以下代码就可以屏蔽了RECNET_APP_KEY
private static final String ACTION_DISABLE_RECENT_BUTTON = "action_disable_recent_button"; private static final String ACTION_ENABLE_RECENT_BUTTON = "action_enable_recent_button"; @Override protected void onResume() { super.onResume(); Intent intent = new Intent(ACTION_DISABLE_RECENT_BUTTON); sendBroadcast(intent); } @Override protected void onPause() { Intent intent = new Intent(ACTION_ENABLE_RECENT_BUTTON); sendBroadcast(intent); super.onPause(); }
相关文章推荐
- Android 屏蔽recent按键 ,Home按键,Recent按键的监听可以用广播,还有开机启动app
- Android 如何让 app 自行处理 home key
- android 4.0如何屏蔽和重写home键
- Android App中屏蔽(拦截)Home按键及其他按键
- Android捕获home和recent app按键
- Android6.0 源码修改之屏蔽导航栏虚拟按键(Home和RecentAPP)/动态显示和隐藏NavigationBar
- 如何在Android App中屏蔽(拦截)Home按键及其他按键
- 如何在Android App中屏蔽(拦截)Home按键及其他按键
- Android 去掉导航栏的HOME和RECENT_APP,只保留BACK
- Android 4.0里无法屏蔽HOME键
- 如何在Android App中屏蔽(拦截)Home按键及其他按键
- android 4.0以上屏蔽HOME按键
- Android4.0以后屏蔽以及HOME按键事件拦截
- Android定制-------------第三方客户锁屏APK屏蔽Home和ResentApp键
- android屏蔽用户单击home键
- [RK3288][Android6.0] 调试笔记 --- 屏蔽Home/Back Key
- Android 4.0屏蔽状态栏(已解决)
- Android app做成 (home)launcher 只是 应用开发非ROM 开发
- Android中屏蔽 电源键长按、Home键、Home长按
- Android 屏蔽Home键(2.2、2.3/4.0以上)