歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android有關Home按鍵的TYPE_KEYGUARD作用的仿照及其流程說明

Android有關Home按鍵的TYPE_KEYGUARD作用的仿照及其流程說明

日期:2017/3/1 10:46:10   编辑:Linux編程

先看到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. // If a system window has focus, then it doesn't make sense
  3. // right now to interact with applications.
  4. WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
  5. if (attrs != null) {
  6. final int type = attrs.type;
  7. if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
  8. || type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
  9. // the "app" is keyguard, so give it the key
  10. return false;
  11. }
  12. final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
  13. for (int i=0; i<typeCount; i++) {
  14. if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {
  15. // don't do anything, but also don't pass it to the app
  16. return true;
  17. }
  18. }
  19. }
從上面的注釋可以看到注釋:// 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. return super.onKeyDown(keyCode, event);
  11. }
  12. /*按鍵彈起*/
  13. public boolean onKeyUp(int keyCode, KeyEvent event)
  14. {
  15. switch (keyCode)
  16. {
  17. case KeyEvent.KEYCODE_HOME:
  18. DisplayToast("HOME鍵彈起");
  19. break;
  20. }
  21. return super.onKeyUp(keyCode, event);
  22. }
這裡就產生了疑問:一、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. /** * Called when the main window associated with the activity has been
  34. * attached to the window manager.
  35. * See {@link View#onAttachedToWindow() View.onAttachedToWindow()}
  36. * for more information.
  37. * @see View#onAttachedToWindow
  38. */
  39. public void onAttachedToWindow() {
  40. this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
  41. super.onAttachedToWindow();
  42. }
  43. onWindowFocusChanged(boolean) 當窗口包含的view獲取或失去焦點時觸發
  44. onAttachedToWindow() 當view被附著到一個窗口時觸發
Copyright © Linux教程網 All Rights Reserved