应用获取Power Key/Home Key按键事件(framework层)

背景

有时候我们在开发的时候需要在应用中获取Power Key,Home Key等按键事件,但是我们知道这些按键属于特殊的按键,应用中并不能获取到。

这些按键在framework层已经被处理掉了,因此我们需要通过一些方法来让应用能获取到。

原理

对于Home Key, Power key这些特殊的按键,应用是不应该获取到的,因为这些按键需要处理一些特殊的事情。
应用获取到后可能会造成隐患,比如按Home 键后不能回到Launcher。
安卓在进行按键分发之前,会被PhoneWindowManager先处理,处理方法为interceptKeyBeforeQueueing(KeyEvent event, int policyFlags)
在这个方法中会决定哪些按键继续往上传递,哪些直接处理调。因此Home Key, Power key在这里就被处理了,不会继续向上传递,因此应用是不能获取到其按键事件的。

实现

既然是在PhoneWindowManager处理掉的,那么我们可以在interceptKeyBeforeQueueing方法中进行修改,让其可以继续传递。
  • 修改方法如下:
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/po
index b2b7341..0bf2249 100755
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -2780,6 +2780,10 @@ public class PhoneWindowManager implements WindowManagerPolicy {
         // it handle it, because that gives us the correct 5 second
         // timeout.
         if (keyCode == KeyEvent.KEYCODE_HOME) {
+            if (SystemProperties.get("dev.keycode.debug").equalsIgnoreCase("true")) {
+                if (DEBUG) Slog.d(TAG, "Home key pressed in debug.");
+                return ACTION_PASS_TO_USER;
+            }
 
             // If we have released the home key, and didn't do anything else
             // while it was pressed, then it is time to go home!
@@ -5163,6 +5167,11 @@ public class PhoneWindowManager implements WindowManagerPolicy {
             }
 
             case KeyEvent.KEYCODE_POWER: {
+                if (SystemProperties.get("dev.keycode.debug").equalsIgnoreCase("true")) {
+                    if (DEBUG) Slog.d(TAG, "Power key pressed in debug.");
+                    return ACTION_PASS_TO_USER;
+                }
+
                 result &= ~ACTION_PASS_TO_USER;
                 isWakeKey = false; // wake-up will be handled separately
                 if (down) {
  • 使用
在需要获取这些按键事件的应用中首先设置"dev.keycode.debug"属性为true。

SystemProperties.set("dev.keycode.debug", "true");

在onKeyDown(int keyCode, KeyEvent event)方法中就可以获取到这些按键事件了。
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Building.Solutions.with.Microsoft.Dot.NET.Compact.Framework,Architecture & Best Practices for Mobile Develop 英文版<br>"If you've been looking for one book on the .NET Compact Framework that will teach you everything you need to know, look no further--this is it!"<br>-Derek Ferguson, Chief Technology Evangelist, Expand Beyond Corporation<br><br>Whether you are an architect, developer, or manager, Building Solutions with the Microsoft .NET Compact Framework is your guide to creating effective solutions for mobile devices with .NET. Authors Dan Fox and Jon Box walk you through four essential architectural concepts and programming techniques, using extensive examples and code listings to show you how to develop more robust mobile development projects. <br><br>The book briefly describes the context, architecture, and features of both the Framework and Smart Device Programmability (SDP). The heart of the book is its in-depth coverage of key architectural concepts, including local data handling, remote data access (RDA) architectures, robust data caching with SQL Server 2000 Windows CE Edition 2.0 (SQL CE 2.0), and synchronization options. The focus then shifts to localization, security, and deployment, and the final chapter steers you away from potential pitfalls.<br><br>You will find helpful pointers to further resources throughout, and a companion Web site includes the source code and links to more information. <br><br>This book can be read cover to cover or the focus can be narrowed to key points of interest. It can also be used as a reference, with answers to questions such as:<br><br>When should you architect and build your applications using the Compact Framework and SDP? Chapter 1 <br><br>What factors besides cost should you consider when planning your company's first Smart Device application? Chapter 4 <br><br>Can RDA allow SQL CE to interact with database products other than SQL Server? Chapter 7 <br><br>How should you handle differences in time zone for users of a world-ready application? Chapter 8 <br><br>How can you overcome the Compact Framework's inability to marshal complex objects within structures? Chapter 11<br><br>Both comprehensive and concise, Building Solutions with the Microsoft .NET Compact Framework shows you how to bring the power of .NET to your mobile applications.<br><br>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值