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