Android implements broadcast listening home key operation

Source: Internet
Author: User

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

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.