Android uses broadcast monitor to press home and power key

Source: Internet
Author: User

 PackageCc.testhome;ImportCc.testhome.HomeKeyObserver.OnHomeKeyListener;ImportCc.testhome.PowerKeyObserver.OnPowerKeyListener;ImportAndroid.os.Bundle;Importandroid.app.Activity;/*** Demo Description: * Use the radio to listen to the home button press and long press the home button * using the Radio listening Power Key press (turn off the screen) * Reference: * 1http://blog.csdn.net/q445697127/article/details/8432513* 2http://blog.csdn.net/watt520/article/details/18959897* 3http://blog.csdn.net/lfdfhl/article/details/9903693* Thank you very much*/ Public classMainactivityextendsActivity {PrivateHomekeyobserver Mhomekeyobserver; PrivatePowerkeyobserver Mpowerkeyobserver; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main);    Init (); }         Private voidinit () {Mhomekeyobserver=NewHomekeyobserver ( This); Mhomekeyobserver.sethomekeylistener (NewOnhomekeylistener () {@Override Public voidonhomekeypressed () {System.out.println ("----> Press the Home button"); } @Override Public voidonhomekeylongpressed () {System.out.println ("----> Long press the Home key");        }        });          Mhomekeyobserver.startlisten (); //////////////////////////////////////////Mpowerkeyobserver=NewPowerkeyobserver ( This); Mpowerkeyobserver.sethomekeylistener (NewOnpowerkeylistener () {@Override Public voidonpowerkeypressed () {System.out.println ("----> Press the Power key");        }        });    Mpowerkeyobserver.startlisten (); } @Overrideprotected voidOnDestroy () {Super. OnDestroy ();                  Mhomekeyobserver.stoplisten (); //////////////////////////////////////////Mpowerkeyobserver.stoplisten (); } }

 PackageCc.testhome;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter; Public classHomekeyobserver {PrivateContext Mcontext; PrivateIntentfilter Mintentfilter; PrivateOnhomekeylistener Monhomekeylistener; PrivateHomekeybroadcastreceiver Mhomekeybroadcastreceiver;  PublicHomekeyobserver (Context context) { This. Mcontext =context; }         //register a broadcast recipient     Public voidStartlisten () {Mintentfilter=NewIntentfilter (intent.action_close_system_dialogs); Mhomekeybroadcastreceiver=NewHomekeybroadcastreceiver ();        Mcontext.registerreceiver (Mhomekeybroadcastreceiver, Mintentfilter); System.out.println ("----> Start Listening"); }         //Cancel Broadcast recipient     Public voidStoplisten () {if(mhomekeybroadcastreceiver!=NULL) {mcontext.unregisterreceiver (mhomekeybroadcastreceiver); System.out.println ("----> Stop Listening"); }    }         //External Exposure Interface     Public voidSethomekeylistener (Onhomekeylistener homekeylistener) {Monhomekeylistener=Homekeylistener; }     //Callback Interface     Public InterfaceOnhomekeylistener { Public voidonhomekeypressed ();  Public voidonhomekeylongpressed (); }     //Broadcast recipient    classHomekeybroadcastreceiverextendsbroadcastreceiver{FinalString System_dialog_reason_key = "REASON"; //Press the home button        FinalString System_dialog_reason_home_key = "HomeKey"; //Long Press the home key        FinalString System_dialog_reason_recent_apps = "Recentapps"; @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&& Monhomekeylistener! =NULL) {                    if(Reason.equals (System_dialog_reason_home_key)) {monhomekeylistener.onhomekeypressed (); } Else if(Reason.equals (System_dialog_reason_recent_apps)) {monhomekeylistener.onhomekeylongpressed (); }                }            }        }    }      }
 PackageCc.testhome;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter; Public classPowerkeyobserver {PrivateContext Mcontext; PrivateIntentfilter Mintentfilter; PrivateOnpowerkeylistener Monpowerkeylistener; PrivatePowerkeybroadcastreceiver Mpowerkeybroadcastreceiver;  PublicPowerkeyobserver (Context context) { This. Mcontext =context; }         //register a broadcast recipient     Public voidStartlisten () {Mintentfilter=NewIntentfilter (Intent.action_screen_off); Mpowerkeybroadcastreceiver=NewPowerkeybroadcastreceiver ();        Mcontext.registerreceiver (Mpowerkeybroadcastreceiver, Mintentfilter); System.out.println ("----> Start Listening"); }         //Cancel Broadcast recipient     Public voidStoplisten () {if(mpowerkeybroadcastreceiver!=NULL) {mcontext.unregisterreceiver (mpowerkeybroadcastreceiver); System.out.println ("----> Stop Listening"); }    }         //External Exposure Interface     Public voidSethomekeylistener (Onpowerkeylistener powerkeylistener) {Monpowerkeylistener=Powerkeylistener; }     //Callback Interface     Public InterfaceOnpowerkeylistener { Public voidonpowerkeypressed (); }     //Broadcast recipient    classPowerkeybroadcastreceiverextendsbroadcastreceiver{@Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); if(Action.equals (Intent.action_screen_off)) {monpowerkeylistener.onpowerkeypressed (); }        }    }      }
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"><!--Main.xml -     <TextViewandroid:layout_centerinparent= "true"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Listen to the home key and the Power key with Radio"> </TextView></Relativelayout>

Android uses broadcast monitor to press home and power key

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.