4 ways to monitor the home key in Android summary _android

Source: Internet
Author: User

Yesterday you need to handle a problem and you need to listen for the home key. The first thought was to use the Onkeydonwn method. But found that home cannot handle this, ONKEYDONWN can handle menu and back keys, but home cannot. Because the home key is a system key, the situation is special.
Look at the information on the Internet, said the following method can be. (Not really)

Copy Code code as follows:

@Override
public void Onattachedtowindow () {
TODO auto-generated Method Stub
LOG.D ("Aeon", "Onattachedtowindow");
This.getwindow (). SetType (WindowManager.LayoutParams.TYPE_KEYGUARD);
Super.onattachedtowindow ();
}

Not after you've added permission.
Copy Code code as follows:

<uses-permission android:name= "Android.permission.DISABLE_KEYGUARD" >
</uses-permission>


After several experiments (android4.1 and android4.2), it was found that in a single activity there are several ways to use this:

method 1:onsaveinstancestate Method
The following method can handle the home listening problem.
Copy Code code as follows:

protected void Onsaveinstancestate (Bundle outstate)

But this method is not very good, does not recommend

Method 2:onuserleavehint Method

Copy Code code as follows:

@Override
protected void Onuserleavehint () {
LOG.D ("Aeon", "Onuserleavehint");
Super.onuserleavehint ();
}

This method will be executed before onsaveinstancestate, and this method is more appropriate according to the API explanation.

Method 3:action_close_system_dialogs

Action_close_system_dialogs can be used in radio monitoring
Copy Code code as follows:

Register receiver

Homekeyeventbroadcastreceiver receiver = new Homekeyeventbroadcastreceiver ();
Registerreceiver (receiver, new Intentfilter (
Intent. Action_close_system_dialogs));

method 4:framework Phonewindowmanager.java processing

To fully monitor the home key needs to be handled at the framework layer.
/frameworks/base/policy/src/com/android/internal/policy/impl/phonewindowmanager.java
Inside to modify the private void Handlelongpressonhome () method.

Copy Code code as follows:

private void Handlelongpressonhome () {
We can ' t initialize this in init () since the configuration hasn ' t been loaded.
if (Mlongpressonhomebehavior < 0) {
Mlongpressonhomebehavior
= Mcontext.getresources (). Getinteger (R.integer.config_longpressonhomebehavior);
if (Mlongpressonhomebehavior < long_press_home_nothing | |
Mlongpressonhomebehavior > long_press_home_recent_system_ui) {
Mlongpressonhomebehavior = long_press_home_nothing;
}
}

if (Mlongpressonhomebehavior!= long_press_home_nothing) {
PERFORMHAPTICFEEDBACKLW (null, hapticfeedbackconstants.long_press, FALSE);
Sendclosesystemwindows (System_dialog_reason_recent_apps);

Eat the longpress so it won ' t dismiss recent apps
The user lets go of the home key
Mhomelongpressed = true;
}

if (Mlongpressonhomebehavior = = Long_press_home_recent_dialog) {
Showorhiderecentappsdialog (Recent_apps_behavior_show_or_dismiss);
else if (Mlongpressonhomebehavior = = long_press_home_recent_system_ui) {
try {
Istatusbarservice StatusBar = Getstatusbarservice ();
if (StatusBar!= null) {
Statusbar.togglerecentapps ();
}
catch (RemoteException e) {
SLOG.E (TAG, "remoteexception when showing recent apps", E);
Re-acquire status bar service next time it is needed.
Mstatusbarservice = null;
}
}
}

Just make a corresponding deal in the Handlelongpressonhome, OK.

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.