AudioManager has the following method:
IsWiredHeadsetOn ();
If the headset is inserted, true is returned; otherwise, false is returned;
Of course, you must add a permission. Otherwise, false is always returned.
<Uses-permission android: name = "android. permission. MODIFY_AUDIO_SETTINGS"/>
I started to catch up with the source code for a long time. I found the process of real-time detecting the insertion and removal of headphones, but it is not very helpful for me.
Real-time Monitoring of headset insertion and removal:
The system sends Intent broadcast every time the headset is inserted and pulled out,
Therefore, you only need to use a receiver to intercept the broadcast intent (the obtained action is android. intent. action. HEADSET_PLUG.
This aggreger must be registered using code, but not written into manifest using the memory writing method.
Detects the insertion and unplugging of headphones in Android, that is, creating a Broadcast Receiver to listen to "android. intent. action. HEADSET_PLUG" Broadcast.
However, adding a <receiver ER> tag directly to AndroidManifest. xml is invalid, for example:
[Html]
Copy codeThe Code is as follows: <Cycler android: name = ". headsetplugreceivreceiver">
<Intent-filter>
<Action android: name = "android. intent. action. HEADSET_PLUG" android: enabled = "true"> </action>
</Intent-filter>
</Cycler>
You will find that the onReceive event of the Receiver will never be triggered. The solution is to manually write the code to register the broadcast.
First, create a subclass of BroadcastReceiver to listen to the plug-in and pull-out of the headset:
[Java]Copy codeThe Code is as follows: public class HeadsetPlugReceiver extends BroadcastReceiver {
Private static final String TAG = "headsetplugreceivreceiver ";
@ Override
Public void onReceive (Context context, Intent intent ){
If (intent. hasExtra ("state ")){
If (intent. getIntExtra ("state", 0) = 0 ){
Toast. makeText (context, "headset not connected", Toast. LENGTH_LONG). show ();
}
Else if (intent. getIntExtra ("state", 0) = 1 ){
Toast. makeText (context, "headset connected", Toast. LENGTH_LONG). show ();
}
}
}
}
Then, register and listen to the broadcast in onCreate () of the Activity that needs to listen to the event, and do not forget to log off listening to the broadcast in onDestroy:
[Java]Copy codeThe Code is as follows: public class TestHeadSetPlugActivity extends Activity {
Private HeadsetPlugReceiver headsetplugreceivreceiver;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
/* Register receiver */
RegisterHeadsetPlugReceiver ();
}
Private void registerheadsetplugreceivreceiver (){
Headsetplugreceivreceiver = new headsetplugreceivreceiver ();
IntentFilter intentFilter = new IntentFilter ();
IntentFilter. addAction ("android. intent. action. HEADSET_PLUG ");
RegisterReceiver (headsetplugreceivreceiver, intentFilter );
}
@ Override
Public void onDestroy (){
UnregisterReceiver (headsetplugreceivreceiver );
Super. onDestroy ();
}
}
In this way, the earphones can be inserted and pulled out.