How does android4.x handle power buttons?

Source: Internet
Author: User
Document directory
  •  
  • 3.1 message timeout Process
  • 3.2 reboot system call Process
1. Introduction

Android4.x processes the power (keyevent. keycode_power) and home (keyevent. keycode_home) keys in phonewindowmanager of the framework and does not transmit these keys to upper-layer applications. If you need to send these keys to activity and service, you need to "Send a broadcast when phonewindowmanager processes these keys, and then process them after the application receives the broadcast ".

If the application only needs to obtain the standby, wake-up, shutdown, and network status change messages, you can listen to the following broadcast messages:
1) Standby:
Broadcast message: Android. Intent. Action. screen_off (CODE)
2) Wake up:
Broadcast message: Android. Intent. Action. screen_on (CODE)
3) shutdown:
Broadcast message: Android. Intent. Action. action_shutdown (XML or code)
4) network status changes:
Broadcast message: android.net. Conn. connectivity_change (XML or code)
Then, call the following isnetworkavailable to obtain the current network status.
Public static Boolean isnetworkavailable (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. Short Press key processing process

The short press power key processing process is shown in:

3. process long press the power key

The process of long press the power key is shown in:

 

3.1 message timeout Process

If you press the power key (more than 500 ms), the message (message. Callback is mpowerlongpress) will be executed. Mpowerlongpress (phonewindowmanager. Java) is defined as follows:

    private final Runnable mPowerLongPress = new Runnable() {        public void run() {            // The context isn't read            if (mLongPressOnPowerBehavior < 0) {                mLongPressOnPowerBehavior = mContext.getResources().getInteger(                        com.android.internal.R.integer.config_longPressOnPowerBehavior);            }            switch (mLongPressOnPowerBehavior) {            case LONG_PRESS_POWER_NOTHING:                break;            case LONG_PRESS_POWER_GLOBAL_ACTIONS:                mPowerKeyHandled = true;                performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);                sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);                showGlobalActionsDialog();                break;            case LONG_PRESS_POWER_SHUT_OFF:                mPowerKeyHandled = true;                performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);                sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);                ShutdownThread.shutdown(mContext, true);                break;            }        }    };

It is an anonymous internal class, it is an object reference of the class implementing runnable, so

New runnable (){

Public void run (){

...

}

};

It includes the objects that define this class (except that this class has no name) and instantiate this class.

When timeout occurs, the execution process is shown in:

3.2 reboot system call Process

Shows the reboot system call process:

 

4. How to Deal with short and long power keys

Long press the power key: the shutdown confirmation dialog box is displayed. (After keydown, if keyup is not received within ms, the shutdown confirmation dialog box is displayed)

Short press the power key: Run <wmactions = 4> During keyup or wake up (run <wmactions = 2> During keydown)

For long-pressed power keys, the interceptkeybeforequeueing function of phonewindowmanager. Java is used for processing. The related code is as follows:

            case KeyEvent.KEYCODE_POWER: {                result &= ~ACTION_PASS_TO_USER;                if (down) {                    if (isScreenOn && !mPowerKeyTriggered                            && (event.getFlags() & KeyEvent.FLAG_FALLBACK) == 0) {                        mPowerKeyTriggered = true;                        mPowerKeyTime = event.getDownTime();                        interceptScreenshotChord();                    }                                       ...                    // Power Key down, set mPowerLongPress executing after 500ms                    interceptPowerKeyDown(!isScreenOn || hungUp                            || mVolumeDownKeyTriggered || mVolumeUpKeyTriggered);                } else {                    mPowerKeyTriggered = false;                    cancelPendingScreenshotChordAction();                    if (interceptPowerKeyUp(canceled || mPendingPowerKeyUpCanceled)) {                        result = (result & ~ACTION_POKE_USER_ACTIVITY) | ACTION_GO_TO_SLEEP;                    }                    // Power key up, remove the mPowerLongPress, that is, if user release                    // power key during 500ms, mPowerLongPress will not be execute, then execute sleep                    mPendingPowerKeyUpCanceled = false;                }                break;            }

    private void interceptPowerKeyDown(boolean handled) {        mPowerKeyHandled = handled;        if (!handled) {            mHandler.postDelayed(mPowerLongPress, ViewConfiguration.getGlobalActionKeyTimeout()/*500ms*/);        }    }    private boolean interceptPowerKeyUp(boolean canceled) {        if (!mPowerKeyHandled) {            mHandler.removeCallbacks(mPowerLongPress);            return !canceled;        }        return false;    }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.