Today in the project, you need to listen to the headset Plug and play, so I wrote the following a small demo, the headset monitoring events to verify. Look directly at the code
PackageCom.example.alert; ImportAndroid.content.BroadcastReceiver; ImportAndroid.content.Context; Importandroid.content.Intent; ImportAndroid.util.Log;ImportAndroid.widget.Toast; Public classHeadsetplugreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {//TODO auto-generated Method Stub intState = Intent.getintextra ("state", 1); intMic = Intent.getintextra ("Microphone", 1); if(State==0 && Mic ==0) {LOG.E ("123", "headset no microphone not Connected"); Toast.maketext (Context,"Headset no microphone not Connected", Toast.length_long). Show (); } Else if(State==0 && Mic ==1) {LOG.E ("123", "headset with microphone not connected"); Toast.maketext (Context,"Headset with microphone not connected", Toast.length_long). Show (); }Else if(State==1 && Mic ==0) {LOG.E ("123", "Headset no microphone connected"); Toast.maketext (Context,"Headset No microphone connected", Toast.length_long). Show (); } Else if(State==1 && Mic ==1) {LOG.E ("123", "Headset with microphone connected"); Toast.maketext (Context,"Headset with microphone connected", Toast.length_long). Show (); } } }
We don't write about the registration of broadcasting in activity, after all, everyone knows
Listen to the broadcast is
Filter.addaction ("Android.intent.action.HEADSET_PLUG");
Look directly at our print log
1. Into the application, plug-in headset, unplug the headset (with microphone)
1 the- - Ten: +:02.815: e/123(32136): Headset no microphone not connected2 the- - Ten: +:29.285: e/123(32136): Headset No microphone connected3 the- - Ten: +:29.355: e/123(32136): Headset with microphone connected4 the- - Ten: +:41.965: e/123(32136): Headset with microphone not connected5 the- - Ten: +:42.995: e/123(32136): Headset no microphone not connected
2. Plug in the headset, enter the application, and unplug the headset (with microphone)
1 the- - Ten: -:49.635: e/123(32136): Headset with microphone connected2 the- - Ten:Wuyi:01.995: e/123(32136): Headset with microphone not connected3 the- - Ten:Wuyi:03.025: e/123(32136): Headset no microphone not connected
3. Go to application, plug-in headset, unplug the headset (no microphone)
1 the- - One:Ten:34.665: e/123(3542): Headset no microphone not connected2 the- - One:Ten:38.985: e/123(3542): Headset No microphone connected3 the- - One:Ten:49.945: e/123(3542): Headset with microphone connected4 the- - One:Ten:50.635: e/123(3542): Headset with microphone not connected5 the- - One:Ten:51.635: e/123(3542): Headset no microphone not connected
4. Plug in the headset, enter the application, and unplug the headset (no microphone)
1 -£º: 27.815: e/123(2838): Headset No Microphone Not connected 2- £ º 30.865: e/123(2838): Headset No microphone connected
From the above results we can draw the following conclusions
1) The radio listening for Wired headphones (with or without microphones) is Intent.action_headset_plug
2) Just enter this application, will listen to the headset, do not understand why, but at least as an initial condition to judge
3) This broadcast can simultaneously monitor whether the plugged-in headset is wired or wireless. However, it seems that wired and wireless plug-in will be wired and wireless judgment, will be sent two times broadcast
4) state-Headphone Insert Status 0-Unplug 1-insert
microphone-there is no mic 0-No 1-there
headset-Monitor wired headphone Plug and Unplug