Skip to content Skip to sidebar Skip to footer

Doing Something If It Detects That Headphones Are Plugged In

I'm wanting to make it so if headphones are plugged in the device something happens such as a notification icon appears. (I already have the notification icon stuff done) But I can

Solution 1:

Add a <receiver> in your manifest that listens for the ACTION_HEADSET_PLUG broadcast. The documentation shows Intent extras that you can use to find out if the headset is plugged in (state), etc.

Solution 2:

I think your question might have some duplicates:

  1. Check whether headphones are plugged in
  2. Android: Checking if headphones are plugged in

The method you want seems to have be deprecated in API level 14. The documentation says to use it only to check whether the headset it connected or not. So for your purposes, that will fit. However, to check whether audio is being played though it, you might need a different solution.

From your comment, it looks like you want to know how to actually use it. The function returns true or false to put it in an if statement as the argument and you'll be set.

Solution 3:

Solution 4:

I found the solution.

AudioManager audio=(AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        if(audio.isWiredHeadsetOn()){
             Toast.makeText(this,"Headset is Connected",Toast.LENGTH_SHORT).show();
        }else{
             Toast.makeText(this,"Headset is Not Connected",Toast.LENGTH_SHORT).show();
        }

Thanks guys!

Post a Comment for "Doing Something If It Detects That Headphones Are Plugged In"