How to detect when a user plugs headset on android device? (Opposite of ACTION_AUDIO_BECOMING_NOISY) How to detect when a user plugs headset on android device? (Opposite of ACTION_AUDIO_BECOMING_NOISY) android android

How to detect when a user plugs headset on android device? (Opposite of ACTION_AUDIO_BECOMING_NOISY)


How about this call:http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUGwhich I found atDroid Incredible Headphones Detection?

The updated code I see in your question now isn't enough. That broadcast happens when the plugged state changes, and sometimes when it doesn't, according to Intent.ACTION_HEADSET_PLUG is received when activity starts so I would write:

package com.example.testmbr;import android.os.Bundle;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.util.Log;public class MainActivity extends Activity  {private static final String TAG = "MainActivity";private MusicIntentReceiver myReceiver;@Override protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    myReceiver = new MusicIntentReceiver();}@Override public void onResume() {    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);    registerReceiver(myReceiver, filter);    super.onResume();}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");            }        }    }}@Override public void onPause() {    unregisterReceiver(myReceiver);    super.onPause();}}

The AudioManager.isWiredHeadsetOn() call which I earlier recommended turns out to be deprecated since API 14, so I replaced it with extracting the state from the broadcast intent. It's possible that there could be multiple broadcasts for each plugging or unplugging, perhaps because of contact bounce in the connector.


I haven't worked with this, but if I'm reading the docs right, ACTION_AUDIO_BECOMING_NOISY is for letting an app know that the audio input might start hearing the audio output. When you unplug the headset, the phone's mic might start picking up the phone's speaker, hence the message.

On the other hand, ACTION_SCO_AUDIO_STATE_UPDATED is designed to let you know when there is a change in the connection state of a bluetooth device.

That second one is probably what you want to listen for.