高通 Android 10/Android 12 应用里面拦截Home键功能

1、由于最近测试部反馈需求产测工具里面需要拦截Home功能

2、可能大家一想到拦截脑海里面可能会想到dispatch/或者onkeydown或者onTouchEvent事件等等,可能网上也有一些文章比如像这种Android拦截HOME键_android 屏蔽home键_天残神功的博客-CSDN博客

3、Android10 在PhoneWindowManager里面interceptKeyBeforeDispatchingInner方法去拦截Home键 return 0就 ok 代码如下

frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -2674,6 +2674,10 @@ public class PhoneWindowManager implements WindowManagerPolicy {
             return 0;
         }
 
+        if (keyCode == KeyEvent.KEYCODE_HOME && win != null && "xxx".equals(win.getOwningPackage())) {
+            return 0;
+        }

4、Android 12 也是在PhoneWindowManager里面interceptKeyBeforeDispatching 去拦截Home键 代码如下

--- /frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ /frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -229,6 +229,8 @@ import java.io.FileReader;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.HashSet;
+import java.util.List;
+import android.content.ComponentName;
 
 /**
  * WindowManagerPolicy implementation for the Android phone UI.  This
@@ -2607,7 +2609,23 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                         Log.d("ZM", "interceptKeyBeforeDispatching KeyEvent="+event.toString());
 
         }
-
+               try {
+                 List tasks = ActivityManager.getService().getTasks(1);
+          ComponentName componentInfo = tasks.get(0).topActivity;
+                 Log.d(TAG, "interceptKeyTi keyCode=" + keyCode + " down=" + down + " repeatCount="
+                    + repeatCount + " keyguardOn=" + keyguardOn + " canceled=" + canceled);
+                        Log.d("ZM", "interceptKeyBeforeDispatching KeyEvent="+event.toString()+"||"+componentInfo.getPackageName());
+         if (keyCode == KeyEvent.KEYCODE_HOME && "xxx".equals(componentInfo.getPackageName())) {
+                        if(DEBUG_INPUT){
+                        Log.d("ZM", "interceptKeyBeforeDispatching Block Home key" + componentInfo.getPackageName());
+                        Log.d("ZM", "interceptKeyBeforeDispatching Block Home key2"+keyCode);
+                        }
+            return 0;
+        }
+               }catch (Exception e){
+                        e.printStackTrace();
+                         Log.e(TAG,"RemoteException error");

5、到这里基本结束了,然后编译aosp源码 烧录 然后在产测工具里面是不会退到主页,在其他应用功能正常 这里只是抛砖引玉如果要拦截修改其他按键方法类似,大家都可以举一反三,转载请注明出处,谢谢!有需要学习可以加我微信kdanmobile高通 Android 10/Android 12 应用里面拦截Home键功能_KdanMin的博客-CSDN博客

你可能感兴趣的:(framework,android,android,studio,ide)