Android Home Key listening Encapsulation

Source: Internet
Author: User

As we all know, the following two methods cannot be used to listen to the Return key event:

@Overridepublic void onBackPressed() {//do something//super.onBackPressed();}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if(keyCode == KeyEvent.KEYCODE_BACK){//do something}return super.onKeyDown(keyCode, event);}

As a matter of course, the method for listening to the Home Key is: If (keycode = keyevent. keycode_home ). But in fact, this is not feasible, because at this time, the Home key message has been intercepted at the framework layer, so, in the application, the Home key message cannot be monitored through this method. So how can we capture and handle the Home key event? In fact, the system sends a broadcast to us. Next, we will share with you the encapsulated Method for listening to the Home key:

Homewatcher class:

/*** Home Key listening encapsulation ** @ author way **/public class homewatcher {static final string tag = "homewatcher"; private context mcontext; private intentfilter mfilter; private listener mlistener; private innerrecevier mrecevier; // callback interface public interface listener {public void onhomepressed (); Public void onhomelongpressed ();} public homewatcher (context) {mcontext = context; mfilter = New intentfilter (intent. action_close_system_dialogs);}/*** set listener ** @ Param listener */Public void listener (onhomepressedlistener listener) {mlistener = listener; mrecevier = new innerrecevier ();} /*** start listening and register broadcast */Public void startwatch () {If (mrecevier! = NULL) {mcontext. registerreceiver (mrecevier, mfilter) ;}}/*** stop listening, cancel broadcasting */Public void stopwatch () {If (mrecevier! = NULL) {mcontext. unregisterreceiver (mrecevier) ;}}/*** broadcast receiver */class innerrecevier extends broadcastreceiver {final string system_dialog_reason_key = "reason"; final string comment = "globalactions "; final string system_dialog_reason_recent_apps = "recentapps"; final string system_dialog_reason_home_key = "homekey"; @ overridepublic void onreceive (context, intent int Ent) {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 mlistener. onhomepressed ();} else if (reason. equals (system_dialog_reason_recent_apps) {// long press the Home Key mlistener. onhomelongpressed ();}}}}}}}

Next, we need to enable the listener in the application. The following uses the application in the activity as an example:

/*** Application homewatcher example ** @ author way **/public class mainactivity extends activity {Private Static final string tag = "mainactivity"; private homewatcher mhomewatcher; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); mhomewatcher = new homewatcher (this); mhomewatcher. setonhomepressedlistener (New onhomepressedlisten Er () {@ overridepublic void onhomepressed () {log. E (TAG, "onhomepressed") ;}@ overridepublic void onhomelongpressed () {log. E (TAG, "onhomelongpressed") ;}}); mhomewatcher. startwatch () ;}@ overrideprotected void onpause () {super. onpause (); mhomewatcher. stopwatch (); // stop listening in onpause; otherwise, an error will be reported .}}
Related Article

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.