3

I want to stop the currently running MusicPlayer when the user unplugs the headphones (both wired & bluetooth).

I came across sevral posts where use of:

isWiredHeadsetOn(), isBluetoothA2dpOn()

is suggested.

But Android docs says isWiredHeadsetOn() is deprecated. What alternative should I use?

Thanks

reiley
  • 3,759
  • 12
  • 58
  • 114

4 Answers4

2

From the docs:

isWiredHeadsetOn() - This method is deprecated. Use only to check is a headset is connected or not.

It sounds like it is still recommended for what you're doing, though the docs are worded poorly

Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149
  • I tried the following code, but it always shows 'Not Connected' `protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.handsetcheck); TextView tvHeadsetStatus = (TextView) findViewById(R.id.tvHeadsetStatus); AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); if(audioManager.isWiredHeadsetOn()) { tvHeadsetStatus.setText("Connected"); }else{ tvHeadsetStatus.setText("Not Connected"); } }` – reiley Jun 25 '12 at 19:49
  • Got it thanks to [this](http://stackoverflow.com/questions/6249884/audiomanager-iswiredheadseton-is-not-working-in-android) – reiley Jun 25 '12 at 20:04
1

I just read a post where someone was suggesting registering for the ACTION_HEADSET_PLUG broadcast event.

You can apparently get the state of it from: intent.getIntExtra("state", 0));

John Kane
  • 4,383
  • 1
  • 24
  • 42
1

I use AudioManager.ACTION_AUDIO_BECOMING_NOISY event in my app. Create your custom broadcast receiver:

private class HeadsetIntentReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context cntx, Intent intent) 
    {
        String action = intent.getAction();

        if(action.compareTo(AudioManager.ACTION_AUDIO_BECOMING_NOISY) == 0)
        {
        }
    }
};  /* end HeadsetIntentReceiver  */

Then register receiver:

headsetReceiver = new HeadsetIntentReceiver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
mParentActivity.registerReceiver(headsetReceiver, mIntentFilter);

Don't forget to unregister it later.

user1777060
  • 263
  • 4
  • 8
0

According to the documentation for it, it's deprecated but suggested to be used ONLY for checking to see if a headset is connected or not. Should be fine to use.

JohnP
  • 402
  • 1
  • 8
  • 25