Android Back Home key monitor

Source: Internet
Author: User

Android Back Home key monitor

Back key monitoring

The back key is easier to listen to, and can be intercepted in multiple systems, such as the following events in the activity:

    @Override public void onbackpressed () {//super.onbackpressed ();//comment out this line, the back key does not exit activity LOG.I (log_t    AG, "onbackpressed"); } @Override public boolean dispatchkeyevent (KeyEvent event) {log.i (Log_tag, "Dispatchkeyevent:keycode--")        + Event.getkeycode ());    Return Super.dispatchkeyevent (event);  } @Override public boolean onKeyDown (int keycode, keyevent event) {log.i (Log_tag, "Onkeydown:keycode--" +        KeyCode);            Switch (keycode) {case keyevent.keycode_back:log.i (Log_tag, "keyevent.keycode_back");        Break            Case KEYEVENT.KEYCODE_MENU:LOG.I (Log_tag, "Keyevent.keycode_menu");        Break            Case KEYEVENT.KEYCODE_HOME:LOG.I (Log_tag, "keyevent.keycode_home");        Cannot receive a break;            Case KEYEVENT.KEYCODE_APP_SWITCH:LOG.I (Log_tag, "Keyevent.keycode_app_switch");        Cannot receive a break; Default:break;        } return Super.onkeydown (KeyCode, event); }

Home key for broadcast monitoring

The home key is not so easy to listen to, because the home key can put the program out of the background, so this event is directly distributed to the system, the system received after the corresponding processing, the home key event is not directly passed to the application inside. So in the above code to listen back key, The corresponding callback is an event that does not receive the home key .

Refer to the blog after the link, the home key monitoring through the registration of the broadcast receiver to achieve , intercept the window to shut down the system action, and then according to the specific parameters inside the intent, analysis of the current home key, the application of the switch, or other function keys.

The receiver implementation is as follows:

Package Com.mengdd.hellohome;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.util.log;public class Homewatcherreceiver extends Broadcastreceiver {private St    atic final String Log_tag = "Homereceiver";    private static final String System_dialog_reason_key = "REASON";    private static final String System_dialog_reason_recent_apps = "Recentapps";    private static final String System_dialog_reason_home_key = "HomeKey";    private static final String System_dialog_reason_lock = "LOCK";    private static final String system_dialog_reason_assist = "ASSIST";        @Override public void OnReceive (context context, Intent Intent) {String action = intent.getaction ();        LOG.I (Log_tag, "onreceive:action:" + action);            if (Action.equals (intent.action_close_system_dialogs)) {//Android.intent.action.CLOSE_SYSTEM_DIALOGS String reason = Intent.getstringextra (System_dialog_reason_key);           LOG.I (Log_tag, "reason:" + reason);            if (System_dialog_reason_home_key.equals (REASON)) {//short press the HOME key log.i (Log_tag, "HomeKey"); } else if (System_dialog_reason_recent_apps.equals (REASON)) {//Long press home key or activity toggle            Key LOG.I (Log_tag, "long press home key or activity switch"); } else if (System_dialog_reason_lock.equals (REASON)) {//Lock screen log.i (Log_tag, "lock            "); } else if (System_dialog_reason_assist.equals (REASON)) {//Samsung long press the home key log.i            (Log_tag, "assist"); }        }    }}

Note that different phone keys are different, so you need to make a distinction between different reasons.

Home key monitoring Broadcast registration

There are two ways to register a broadcast receiver, one is static registration, which is written in manifest, and the other is dynamic registration, which is registered in Java code.

The receiver that listens to the home key above, statically registers as follows:

        <receiver android:name= "Com.mengdd.hellohome.HomeWatcherReceiver" >            <intent-filter>                < Action android:name= "Android.intent.action.CLOSE_SYSTEM_DIALOGS"/>            </intent-filter>        </ Receiver>

However, it is found that static registration does not work , that is, the OnReceive callback is not received.

Use dynamic registration:

    private static Homewatcherreceiver mhomekeyreceiver = null;    private static void Registerhomekeyreceiver (context context) {        log.i (Log_tag, "Registerhomekeyreceiver");        Mhomekeyreceiver = new Homewatcherreceiver ();        Final Intentfilter homefilter = new Intentfilter (intent.action_close_system_dialogs);        Context.registerreceiver (Mhomekeyreceiver, homefilter);    }    private static void Unregisterhomekeyreceiver (context context) {        log.i (Log_tag, "Unregisterhomekeyreceiver");        if (null! = Mhomekeyreceiver) {            context.unregisterreceiver (mhomekeyreceiver);        }    }

  

In the Onresume and onpause of the activity are called separately:

    @Override    protected void Onresume () {        super.onresume ();        Registerhomekeyreceiver (this);    }    @Override    protected void OnPause () {        unregisterhomekeyreceiver (this);        Super.onpause ();    }

Of course, you can also register and log out at other appropriate times as needed.

Resources

http://blog.csdn.net/way_ping_li/article/details/8953622

Android Back Home key monitor

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.