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