android 4.0 屏蔽 HOME_KEY 和 RECENT_APP_KEY

2024-01-12 16:48

本文主要是介绍android 4.0 屏蔽 HOME_KEY 和 RECENT_APP_KEY,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在项目中需要屏蔽虚拟按键,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() 方法中做修改

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 keyreturn 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 appreturn -1;}}//以下条件屏蔽HOMEKEYfinal 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 broadcastsIntentFilter 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 wellmRecentsPanel.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";@Overrideprotected void onResume() {super.onResume();Intent intent = new Intent(ACTION_DISABLE_RECENT_BUTTON);sendBroadcast(intent);}@Overrideprotected void onPause() {Intent intent = new Intent(ACTION_ENABLE_RECENT_BUTTON);sendBroadcast(intent);super.onPause();}


这篇关于android 4.0 屏蔽 HOME_KEY 和 RECENT_APP_KEY的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/598516

相关文章

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

SQL 外键Foreign Key全解析

《SQL外键ForeignKey全解析》外键是数据库表中的一列(或一组列),用于​​建立两个表之间的关联关系​​,外键的值必须匹配另一个表的主键(PrimaryKey)或唯一约束(UniqueCo... 目录1. 什么是外键?​​ ​​​​2. 外键的语法​​​​3. 外键的约束行为​​​​4. 多列外键​

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

浅谈Redis Key 命名规范文档

《浅谈RedisKey命名规范文档》本文介绍了Redis键名命名规范,包括命名格式、具体规范、数据类型扩展命名、时间敏感型键名、规范总结以及实际应用示例,感兴趣的可以了解一下... 目录1. 命名格式格式模板:示例:2. 具体规范2.1 小写命名2.2 使用冒号分隔层级2.3 标识符命名3. 数据类型扩展命

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮