I wrote a service to listen to the home key operation via radio in the services:
public class Homeservice extends service{
Private Monitohomereceiver Mhomebroadcastreceiver;
@Override
Public IBinder Onbind (Intent Intent) {
return null;
}
@Override
public void OnCreate () {
Super.oncreate ();
Mhomebroadcastreceiver = new Monitohomereceiver ();
/**
* Android is inconsistent with the usual key message handling at the bottom of the screen:
* 1, the Search button message is received in onkeydown or onkeyup;
* 2, the message of the menu button is received in Oncreateoptionsmenu, onkeydown or OnKeyUp method;
* 3. Messages that return keystrokes can be received in the onbackpressed, onkeydown, or onkeyup methods.
* For home key message processing, can not be received through onkeydown, OnKeyUp, Android also does not provide a proprietary method to receive key messages
* But there is always a way, when you click on the Home button will be issued an ACTION to Intent.action_close_system_dialogs broadcast, by registering it to listen to the home button message
*/
Intentfilter homefilter = new Intentfilter (intent.action_close_system_dialogs);
Registerreceiver (Mhomebroadcastreceiver, Homefilter);
}
public class Monitohomereceiver extends broadcastreceiver{
Final String Home_dialog_reason = "Homereason";
Final String home_dialog_reason_home = "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 (Home_dialog_reason);
if (reason! = null && reason.equals (home_dialog_reason_home)) {
Toast.maketext (Getapplicationcontext (), "Click home Button", Toast.length_short). Show ();
Return
}
}
}
}
@Override
public void OnDestroy () {
Unregisterreceiver (Mhomebroadcastreceiver);
Mhomebroadcastreceiver = null;
Super.ondestroy ();
}
}
Then start the service in the activity:
public class Mainactivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Span style= "White-space:pre" > super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
/** button click event */
Findviewbyid ( R.ID.TEST_HOME_BTN). Setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Intent Intent = new Intent (mainactivity.this,homeservice.class);
StartService (intent);
}
});
}
}
Finally, there is a simple layout file:
<linearlayout xmlns: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"
android:orientation= "Vertical" >
<button
Android:id= "@+id/test_home_btn"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Test Home key"/>
</LinearLayout>
Finally, don't forget to register the Homeservice service with the <Service></Service> tag in Androidmanifest.xml.
Android implements broadcast listening home key operation