Android enables headphone insertion and removal status detection

Source: Internet
Author: User

To detect headphone insertion and removal under Android, you need to create a broadcastreceiver, used to listen for "Android.intent.action.HEADSET_PLUG" broadcasts.


implementation steps:

1. Create a broadcastreceiver subclass and override the OnReceive () method,in this method, the processing logic after receiving the broadcast is written;

2. Create an activity class that uses Registerreceiver () in the OnCreate () methodmethods to register and monitor broadcasts;

3. In activity, rewrite the Ondestory () method and use Unregisterreceiver () to unregister the listening broadcast.

The specific implementation code is as follows:

Broadcast receiver Class

Package Com.leigo.demo.receiver;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import android.widget.toast;/** * Created by Administrator on 2014/8/27. */public class Headsetdetectreceiver extends Broadcastreceiver {    @Override public    void OnReceive (Context Context, Intent Intent) {        String action = intent.getaction ();        if (Intent.ACTION_HEADSET_PLUG.equals (ACTION)) {            if (Intent.hasextra ("state")} {                int state = Intent.getintextra ("state", 0);                if (state = = 1) {                    Toast.maketext (context, "Insert headset", Toast.length_short). Show ();                } else if (state = = 0) {                    Toast.maketext (context, "unplug headphones", toast.length_short). Show ();}}}}    


Package Com.leigo.demo;import Android.app.activity;import Android.content.broadcastreceiver;import Android.content.intent;import Android.content.intentfilter;import Android.os.bundle;import Android.view.Menu; Import Android.view.menuitem;import Com.leigo.demo.receiver.headsetdetectreceiver;public class MainActivity extends    Activity {private Broadcastreceiver receiver;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);  /** * Broadcast action:wired Headset plugged in or unplugged * The intent would have the following extra         Values: * state-0 for Unplugged, 1 for plugged. * Name-headset type, human readable string * microphone-1 if Headset has a microphone, 0 otherwise *        /receiver = new Headsetdetectreceiver ();        Intentfilter intentfilter = new Intentfilter (); Intentfilter.addaction (Intent.action_Headset_plug);    Registerreceiver (receiver, intentfilter);        } @Override protected void OnDestroy () {Super.ondestroy ();    Unregisterreceiver (receiver); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio        n Bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}

Android enables headphone insertion and removal status detection

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.