閱讀707 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android 長按電源鍵和短按電源鍵的詳細處理流程

1 Android

 Android4.x在Framework的PhoneWindowManager對Power(KeyEvent.KEYCODE_POWER)和Home(KeyEvent.KEYCODE_HOME)鍵做了處理,不會把這些鍵傳送上層應用程序。如需要把這些鍵發送給Activity和Service,需要在PhoneWindowManager處理這些鍵時“發送一個廣播出去,然後在應用程序接收到廣播後做處理”。

       如果應用程序隻需要獲取獲取待機、喚醒、關機、網絡狀態變化消息,則可監聽以下廣播消息:
1) 待機:
廣播消息:android.intent.action.SCREEN_OFF (代碼)
2) 喚醒:
廣播消息:android.intent.action.SCREEN_ON (代碼)
3) 關機:
廣播消息:android.intent.action.ACTION_SHUTDOWN (XML或代碼)
4) 網絡狀態變化:
 廣播消息:android.net.conn.CONNECTIVITY_CHANGE (XML或代碼)
                  然後調用下麵的isNetworkAvailable獲取當前網絡狀態。
public static boolean isNetworkAvailable(Context context) { 

        ConnectivityManager mgr = (ConnectivityManager) context  
                .getSystemService(Context.CONNECTIVITY_SERVICE);  
        NetworkInfo[] info = mgr.getAllNetworkInfo();  
        if (info != null) {  
            for (int i = 0; i < info.length; i++) {  
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {  
                    return true;  
                }  
            }  
        }  
        return false;  
    } 

2. 短按Power鍵處理流程

    短按Power鍵處理流程如下圖所示:

 

3. 長按Power鍵處理流程

      長按Power鍵處理流程如下圖所示:       

 

3.1 Message超時處理流程

      如果長按Power鍵(超過500ms),則此消息(Message.callback為mPowerLongPress)將被執行。mPowerLongPress (PhoneWindowManager.java)定義如下:

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. private final Runnable mPowerLongPress = new Runnable() {    
  2.     public void run() {    
  3.         // The context isn't read    
  4.         if (mLongPressOnPowerBehavior < 0) {    
  5.             mLongPressOnPowerBehavior = mContext.getResources().getInteger(    
  6.                     com.android.internal.R.integer.config_longPressOnPowerBehavior);    
  7.         }    
  8.         switch (mLongPressOnPowerBehavior) {    
  9.         case LONG_PRESS_POWER_NOTHING:    
  10.             break;    
  11.         case LONG_PRESS_POWER_GLOBAL_ACTIONS:    
  12.             mPowerKeyHandled = true;    
  13.             performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);    
  14.             sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);    
  15.             showGlobalActionsDialog();    
  16.             break;    
  17.         case LONG_PRESS_POWER_SHUT_OFF:    
  18.             mPowerKeyHandled = true;    
  19.             performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);    
  20.             sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);    
  21.             ShutdownThread.shutdown(mContext, true);    
  22.             break;    
  23.         }    
  24.     }    
  25. };    

它是一個匿名內部類,它是一個實現Runnable的類的對象引用,因此

     new Runnable() {

         public void run(){

         ...

         }

     };

     它包括了定義這個類(隻不過這個類沒有名字)和實例化這個類的對象。

      當超時時,其執行流程如下圖所示:

     

 3.2 reboot係統調用流程

 reboot係統調用流程如下圖所示:

 

 4. 如何處理短按和長按電源鍵

     長按電源鍵:彈出關機確認對話框(KeyDown之後,如果 500ms之內,沒有收到KeyUp則彈出關機確認對話框)

     短按電源鍵:執行待機(KeyUp時執行<wmActions=4>)或喚醒(KeyDown時執行<wmActions=2>)

    

     對於長按電源鍵,在PhoneWindowManager.java的interceptKeyBeforeQueueing函數中進行處理,其相關代碼如下 :

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. case KeyEvent.KEYCODE_POWER: {    
  2.     result &= ~ACTION_PASS_TO_USER;    
  3.     if (down) {    
  4.         if (isScreenOn && !mPowerKeyTriggered    
  5.                 && (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {    
  6.             mPowerKeyTriggered = true;    
  7.             mPowerKeyTime = event.getDownTime();    
  8.             interceptScreenshotChord();    
  9.         }    
  10.            
  11.         ...    
  12.         // Power Key down, set mPowerLongPress executing after 500ms    
  13.         interceptPowerKeyDown(!isScreenOn || hungUp    
  14.                 || mVolumeDownKeyTriggered || mVolumeUpKeyTriggered);    
  15.     } else {    
  16.         mPowerKeyTriggered = false;    
  17.         cancelPendingScreenshotChordAction();    
  18.         if (interceptPowerKeyUp(canceled || mPendingPowerKeyUpCanceled)) {    
  19.             result = (result & ~ACTION_POKE_USER_ACTIVITY) | ACTION_GO_TO_SLEEP;    
  20.         }    
  21.         // Power key up, remove the mPowerLongPress, that is, if user release    
  22.         // power key during 500ms, mPowerLongPress will not be execute, then execute sleep    
  23.         mPendingPowerKeyUpCanceled = false;    
  24.     }    
  25.     break;    
  26. }    
[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. private void interceptPowerKeyDown(boolean handled) {    
  2.     mPowerKeyHandled = handled;    
  3.     if (!handled) {    
  4.         mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout()/*500ms*/);    
  5.     }    
  6. }    
  7.     
  8. private boolean interceptPowerKeyUp(boolean canceled) {    
  9.     if (!mPowerKeyHandled) {    
  10.         mHandler.removeCallbacks(mPowerLongPress);    
  11.         return !canceled;    
  12.     }    
  13.     return false;    
  14. }    

最後更新:2017-04-03 12:55:35

  上一篇:go Java錯誤提示is not an enclosing class
  下一篇:go linux文件係統(一)——VFS的概念