zoukankan      html  css  js  c++  java
  • Android有关Home按键的TYPE_KEYGUARD作用

    先看到PhoneWindowManager中public boolean interceptKeyTi(WindowState win, int code, int metaKeys, boolean down, 
                int repeatCount, int flags) 这个方法的实现,interceptKeyTi你可以暂时理解为WindowManagerService中处理驱动和上层按键实现的过滤器

    1. if (code == KeyEvent.KEYCODE_HOME) {  
    2.   
    3.     // If a system window has focus, then it doesn't make sense   
    4.     // right now to interact with applications.   
    5.     WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;  
    6.     if (attrs != null) {  
    7.         final int type = attrs.type;  
    8.         if (type == WindowManager.LayoutParams.TYPE_KEYGUARD  
    9.                 || type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {  
    10.             // the "app" is keyguard, so give it the key   
    11.             return false;  
    12.         }  
    13.         final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;  
    14.         for (int i=0; i<typeCount; i++) {  
    15.             if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {  
    16.                 // don't do anything, but also don't pass it to the app   
    17.                 return true;  
    18.             }  
    19.         }  
    20.     }  
    从上面的注释可以看到注释:// the "app" is keyguard, so give it the key ,就是说当在应用界面下的时候,按了HOME键而且当前应用的WindowManager.LayoutParams.type的值是WindowManager.LayoutParams.TYPE_KEYGUARD就让直接返回;返回做什么呢,我先告诉大家,这个interceptKeyTi方法被调用的地方的流程后续步骤就是根据这个interceptKeyTi的返回值来判断,如果返回的是false就让当前应用自己去做HOME键的业务处理通过类似下面的代码
    1. /* 按键按下 */  
    2.   public boolean onKeyDown(int keyCode, KeyEvent event)  
    3.   {  
    4.       switch (keyCode)  
    5.       {  
    6.           case KeyEvent.KEYCODE_HOME:  
    7.               DisplayToast("HOME键按下");  
    8.               break;  
    9.           
    10.       }  
    11.       return super.onKeyDown(keyCode, event);  
    12.   
    13.   }  
    14.     
    15.   /*按键弹起*/  
    16.   public boolean onKeyUp(int keyCode, KeyEvent event)  
    17.   {  
    18.       switch (keyCode)  
    19.       {  
    20.           case KeyEvent.KEYCODE_HOME:  
    21.               DisplayToast("HOME键弹起");  
    22.               break;  
    23.            
    24.       }  
    25.         
    26.       return super.onKeyUp(keyCode, event);  
    27.   }  
    这里就产生了疑问:一、WindowManager.LayoutParams.type的值是在应用的哪里初始化的,二、interceptKeyTi方法被调用的地方的流程后续步骤是怎么调应用的HOME键的处理方式的,三、interceptKeyTi方法被调用的地方的流程后续步骤是怎么获取到WindowManager.LayoutParams.type初始化的值的;这三个疑问基本上就是按键的一个流程即怎么通过底层驱动到Activity相应按键事件相应的。

    下面我们来看第一个问题的解答:Activity中有两个可覆盖的方法,都可以做如下的初始化:

    1.     /** 
    2.      * Called when the current {@link Window} of the activity gains or loses 
    3.      * focus.  This is the best indicator of whether this activity is visible 
    4.      * to the user.  The default implementation clears the key tracking 
    5.      * state, so should always be called. 
    6.      *  
    7.      * <p>Note that this provides information about global focus state, which 
    8.      * is managed independently of activity lifecycles.  As such, while focus 
    9.      * changes will generally have some relation to lifecycle changes (an 
    10.      * activity that is stopped will not generally get window focus), you 
    11.      * should not rely on any particular order between the callbacks here and 
    12.      * those in the other lifecycle methods such as {@link #onResume}. 
    13.      *  
    14.      * <p>As a general rule, however, a resumed activity will have window 
    15.      * focus...  unless it has displayed other dialogs or popups that take 
    16.      * input focus, in which case the activity itself will not have focus 
    17.      * when the other windows have it.  Likewise, the system may display 
    18.      * system-level windows (such as the status bar notification panel or 
    19.      * a system alert) which will temporarily take window input focus without 
    20.      * pausing the foreground activity. 
    21.      * 
    22.      * @param hasFocus Whether the window of this activity has focus. 
    23.      *  
    24.      * @see #hasWindowFocus() 
    25.      * @see #onResume 
    26.      * @see View#onWindowFocusChanged(boolean) 
    27.      */  
    28.     public void onWindowFocusChanged(boolean hasFocus) {  
    29.         WindowManager.LayoutParams lp = new WindowManager.LayoutParams();  
    30.         lp.type = WindowManager.LayoutParams.TYPE_KEYGUARD ;  
    31.         this.getWindow().setAttributes(lp);  
    32.     }  
    33.   
    34.   
    35.     /** * Called when the main window associated with the activity has been  
    36.         * attached     to the window manager. 
    37.        * See {@link View#onAttachedToWindow() View.onAttachedToWindow()} 
    38.        * for more information. 
    39.       * @see View#onAttachedToWindow  
    40.      */   
    41.     public void onAttachedToWindow() {     
    42.          this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);         
    43.        super.onAttachedToWindow();    
    44.    }   
    45.   
    46.   
    47. onWindowFocusChanged(boolean) 当窗口包含的view获取或失去焦点时触发   
    48. onAttachedToWindow() 当view被附着到一个窗口时触发 
    49. LayoutParams的构造方式有很多种可以顺带学习下,同时最好学习下它的参数,项目中用到就知道它的重要性了:
      1. public LayoutParams() {  
      2.     super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);  
      3.     type = TYPE_APPLICATION;  
      4.     format = PixelFormat.OPAQUE;  
      5. }  
      6.   
      7. public LayoutParams(int _type) {  
      8.     super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);  
      9.     type = _type;  
      10.     format = PixelFormat.OPAQUE;  
      11. }  
      12.   
      13. public LayoutParams(int _type, int _flags) {  
      14.     super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);  
      15.     type = _type;  
      16.     flags = _flags;  
      17.     format = PixelFormat.OPAQUE;  
      18. }  
      19.   
      20. public LayoutParams(int _type, int _flags, int _format) {  
      21.     super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);  
      22.     type = _type;  
      23.     flags = _flags;  
      24.     format = _format;  
      25. }  
      26.   
      27. public LayoutParams(int w, int h, int _type, int _flags, int _format) {  
      28.     super(w, h);  
      29.     type = _type;  
      30.     flags = _flags;  
      31.     format = _format;  
      32. }  
      33.   
      34. public LayoutParams(int w, int h, int xpos, int ypos, int _type,  
      35.         int _flags, int _format) {  
      36.     super(w, h);  
      37.     x = xpos;  
      38.     y = ypos;  
      39.     type = _type;  
      40.     flags = _flags;  
      41.     format = _format;  
      42. }  
      到这一步为至即为网上很多转的都一样的帖子即屏蔽Home键写一些自己的业务逻辑
  • 相关阅读:
    C/C++字符串函数之复制函数
    tesseract api C++使用例子
    error C2275: “XXX”: 将此类型用作表达式非法
    Socket通信原理探讨(C++为例)
    模拟按键,点击,滑动,在光标处输出字符
    安卓使用Root权限实现后台模拟全局按键、触屏事件方法(类似按键精灵)
    【 转】__try,__except,__finally,__leave异常模型机制
    提高VS2010运行速度的技巧
    解决VS2010子目录中的.cpp文件引用上一级目录的stdafx.h找不到定义的问题
    1009MySQL数据库InnoDB存储引擎Log漫游
  • 原文地址:https://www.cnblogs.com/liulaolaiu/p/11744649.html
Copyright © 2011-2022 走看看