There is a transparent code for determining the connection / disconnection of headphones, tell me how to determine if an external microphone is connected (presence of a microphone at the connected headset)

private class MusicIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) { int state = intent.getIntExtra("state", -1); switch (state) { case 0: Log.d(TAG, "Headset is unplugged"); break; case 1: Log.d(TAG, "Headset is plugged"); break; default: Log.d(TAG, "I have no idea what the headset state is"); } } } IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); registerReceiver(myReceiver, filter); 

    1 answer 1

    You can also get this from intent :

     int mic = intent.getIntExtra("microphone", -1); switch (mic ) { case 0: Log.d(TAG, "No microphone"); break; case 1: Log.d(TAG, "Headset with microphone"); break; default: Log.d(TAG, "Something wrong"); break; } Log.d(TAG, intent.getStringExtra("name", "no name")); 
    • Can you please tell me how such information could be found on developer.android.com . After your answer there was enough imagination in debug mode to look at mExtras for intent. - x555xx
    • one
      @ x555xx In the search on the site enter ACTION_HEADSET_PLUG microphone, and on the first link find developer.android.com/reference/android/media/… . If you just enter ACTION_HEADSET_PLUG in the search, then the first link will not find it, only the second one :) - VAndrJ
    • Thank you very much! figured out) - x555xx