• Android拦截HOME键


    Android拦截按键

    Android拦截HOME按键

    拦截HOME按键需要修改Android的framework层

    • 具体代码文件路径android/frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

    PhoneWindowManager的interceptKeyBeforeDispatching方法,这个方法对按键事件做了二次拦截
    当返回值为-1则说明事件被拦截,返回值为0则说明事件放开执行。

    举例在某个APP中屏蔽对应的HOME按键;

    • 如下代码为识别当前的APP是哪个?
    public String getTopActivity(Context context){                                                                                                            
             ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
              ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
              Log.d(TAG, "pkg:" + cn.getPackageName() + " cls:"+cn.getClassName());//包名加类名
              return cn.getClassName();
         }
    
    public boolean isBackgroundActivity(Context context, String packageName) {
              ActivityManager am = (ActivityManager) context.getSystemService("activity");
             List tasks = am.getRunningTasks(1);
              if (!tasks.isEmpty()) {
                  ComponentName topActivity = ((ActivityManager.RunningTaskInfo) tasks.get(0)).topActivity;
                  Log.w(TAG, "topActivity packageName = " + topActivity.getPackageName());
                  if (!topActivity.getPackageName().equals(packageName)) {
                      Log.w(TAG, packageName + " is background!");
                      return true;
                  }
              }
              Log.w(TAG, packageName + " is foreground!");
              return false;
         }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 如下代码判断针对对应的APP进行HOME按键拦截:
    /** {@inheritDoc} */
         @Override
         public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
    ......
    // 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 (keyCode == KeyEvent.KEYCODE_HOME) {
     
                 // If we have released the home key, and didn't do anything else
                 // while it was pressed, then it is time to go home!
                 if (!down) {
                     //添加的判断当前前台运行APP代码
                     if (!isBackgroundActivity(mContext, PKG_NAME_xxxxx)) {
                             Log.d(TAG, "xxxx app is foreground");
                             if (!getTopActivity(mContext).equals(ACTIVITY_NAME_xxxx)) {                                                          
                                     Log.d(TAG, "xxxxx app is foreground activity");
                                     return -1;
                             }
                            return -1;
                     }
                     
     
                     cancelPreloadRecentApps();
    
                     mHomePressed = false;
                     if (mHomeConsumed) {
                         mHomeConsumed = false;
                         return -1;
                     }
                     ......
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    备注:
    直接在onKeyDown中判断通过keyCode == KeyEvent.KEYCODE_HOME是不能拦截的

    Android拦截BACK按键

    这个在Activity中自行重写回调即可,具体见如下示意代码:

    @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if(keyCode == KeyEvent.KEYCODE_BACK){
            	//按键按下时返回处理逻辑可以放在这里
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
    
        @Override
        public boolean onKeyUp(int keyCode, KeyEvent event) {
            if(keyCode == KeyEvent.KEYCODE_BACK){
                 //按键释放时返回处理逻辑可以放在这里
                return true;
            }
            return super.onKeyUp(keyCode, event);
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    注:在onKeyDown或onKeyUp中使用return true或return false,都会拦截掉返回按键。

  • 相关阅读:
    NLP主要研究哪些方向?
    Linux安装Tomcat最新版
    【python基础】字典详解:如何定义、修改、遍历字典、字典中嵌套字典、列表等
    云原生数据库极致弹性体验 - Amazon Aurora Serverless v2
    如何优雅得关闭协程呢
    HashMap 源码分析
    JVM年轻代(young generation)老年代(old generation tenured)持久代(permanent generation)GC
    opencv 通过滑动条调整阈值处理、边缘检测、轮廓检测、模糊、色调调整和对比度增强参数 并实时预览效果
    TCP/IP协议栈在内核态的好还是用户态的好
    190-Vue中环境变量的配置
  • 原文地址:https://blog.csdn.net/ACM_BMW/article/details/127907254