This example describes how Android listens to the home key. Share to everyone for your reference, specific as follows:
Will go to Android in the home button listening, many people first reaction to rewrite the corresponding activity of the onkeydown () method, monitoring when the key pressed KeyCode for Keycode_home, carry out their own corresponding processing. Such as:
@Override Public
boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = keyevent.keycode_home) {
StopService (mintentservice);
return true;
}
Return Super.onkeydown (KeyCode, event);
}
But such monitoring is not able to hear the home button. Such a method can only be heard Back,menu key. So how do you monitor the home button?
1. Create a new listener to monitor the home key
public class Homelistener {static final String TAG = "Homelistener";
Private context Mcontext;
Private Intentfilter Mfilter;
Private Onhomepressedlistener Mlistener;
Private Innerrecevier Mrecevier;
The callback interface is public interface Onhomepressedlistener {public void onhomepressed ();
public void onhomelongpressed ();
Public Homelistener {mcontext = context;
Mfilter = new Intentfilter (intent.action_close_system_dialogs);
/** * Set Listening * * @param listener/public void Setonhomepressedlistener (Onhomepressedlistener listener) {
Mlistener = listener;
Mrecevier = new Innerrecevier (); /** * Start listening, registration broadcast/public void Startwatch () {if (mrecevier!= null) {Mcontext.registerreceiver (mRe
Cevier, Mfilter); }/** * Stop listening, logout broadcast/public void stopwatch () {if (mrecevier!= null) {mcontext.unregisterreceive
R (Mrecevier);
} class Innerrecevier extends broadcastreceiver{ Final String System_dialog_reason_key = "REASON";
Final String system_dialog_reason_global_actions = "Globalactions";
Final String System_dialog_reason_recent_apps = "Recentapps";
Final String System_dialog_reason_home_key = "HomeKey";
@Override public void OnReceive (context context, Intent Intent) {String action = intent.getaction (); if (Action.equals (intent.action_close_system_dialogs)) {String reason = Intent.getstringextra (System_dialog_reaso
N_key);
if (reason!= null) {//LOG.E (TAG, "action:" + action + ", Reason:" + reason);
if (Mlistener!= null) {if (Reason.equals (System_dialog_reason_home_key)) {//Short press home key
Mlistener.onhomepressed (); else if (reason. Equals (System_dialog_reason_recent_apps)) {//long press the home key MLIs
Tener.onhomelongpressed ();
}
}
}
}
}
}
}
2. Registering the Home key listener in the OnCreate method of the class being used
/**
* Registration of the home button
monitoring
/private void Registerhomelistener () {
mhomewatcher = new Homelistener (this);
Mhomewatcher.setonhomepressedlistener (New Onhomepressedlistener () {
@Override public
void onhomepressed () {
//todo to handle
the click of the Home key LOG.I ("xsl", "0000000000000");
StopService (Mintentservice);
}
@Override public
void onhomelongpressed () {
//todo the processing
log.i ("xsl", "0000000000000") for long pressing the home key;
}
});
Mhomewatcher.startwatch ();
}
3. Stop listening in the OnPause method of the class being used
Mhomewatcher.stopwatch ();
More interested readers of Android-related content can view this site: Android Development Primer and Advanced tutorial, Android resource Operation tips Summary, Android View view tips Summary and Android control usage summary
I hope this article will help you with the Android program.