Listen to the home button under Android

Source: Internet
Author: User

Online to see the demo, pro-test the following machines and equipment, are tested through:

Tested by Mobile: 1. Huawei Glory 3 (Android 4.2.2) 2. Xiaomi 2s (android 4.1.1) 3. Lenovo's mobile phone (Android2.3.5) 4.Google Nexus 5 Simulator (Android 4.4.2)

Core code:

ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.util.Log; Public classHomewatcher {Static FinalString TAG = "WXL"; PrivateContext Mcontext; PrivateIntentfilter Mfilter; PrivateOnhomepressedlistener Mlistener; PrivateInnerrecevier Mrecevier; //Callback Interface     Public InterfaceOnhomepressedlistener { Public voidonhomepressed ();  Public voidonhomelongpressed (); }     PublicHomewatcher (Context context) {Mcontext=context; Mfilter=NewIntentfilter (intent.action_close_system_dialogs); }    /*** Set Monitoring * *@paramListener*/     Public voidSetonhomepressedlistener (Onhomepressedlistener listener) {Mlistener=Listener; Mrecevier=NewInnerrecevier (); }    /*** Start listening, register for the broadcast*/     Public voidStartwatch () {if(Mrecevier! =NULL) {mcontext.registerreceiver (mrecevier, Mfilter); }    }    /*** Stop listening, cancel the broadcast*/     Public voidStopWatch () {if(Mrecevier! =NULL) {mcontext.unregisterreceiver (mrecevier); }    }    /*** Broadcast Recipient*/    classInnerrecevierextendsBroadcastreceiver {FinalString System_dialog_reason_key = "REASON"; FinalString system_dialog_reason_global_actions = "Globalactions"; FinalString System_dialog_reason_recent_apps = "Recentapps"; FinalString System_dialog_reason_home_key = "HomeKey"; @Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); if(Action.equals (intent.action_close_system_dialogs)) {String reason=Intent.getstringextra (System_dialog_reason_key); if(Reason! =NULL) {log.e (TAG,"Action:" + action + ", Reason:" +reason); if(Mlistener! =NULL) {                        if(Reason.equals (System_dialog_reason_home_key)) {//Press the home key brieflymlistener.onhomepressed (); } Else if(reason. Equals (System_dialog_reason_recent_apps)) {//Long Press the home keymlistener.onhomelongpressed (); }                    }                }            }        }    }}

Call Mode:

 Public classMainactivityextendsActivity {Private Static FinalString TAG = "WXL"; PrivateHomewatcher Mhomewatcher; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mhomewatcher=NewHomewatcher ( This); Mhomewatcher.setonhomepressedlistener (NewOnhomepressedlistener () {@Override Public voidonhomepressed () {Toast.maketext (mainactivity. This, "onhomepressed", Toast.length_long). Show (); } @Override Public voidonhomelongpressed () {Toast.maketext (mainactivity. This, "onhomelongpressed", Toast.length_long). Show ();        }        });    Mhomewatcher.startwatch (); }    /*** Re-enter or turn on the screen light*/    protected voidOnrestart () {log.d (TAG,"Onrestart");        Mhomewatcher.startwatch (); Super. Onrestart (); }    /*** Press the home button or turn off the screen light*/    protected voidOnPause () {Super. OnPause (); LOG.D (TAG,"OnPause"); Mhomewatcher.stopwatch ();//stop listening in the OnPause, or you will get an error.     }}

Demo:http://files.cnblogs.com/hsx514/apkdemo.zip

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.