Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if headset is plugged in or not?

Tags:

android

I have used below code to check that headset connected or not in onCreate Mehod.

But every time it gives me output of not connected.

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

Anybody can tell what's wrong with above code.

Thank You.

like image 887
Nirav Avatar asked Oct 09 '22 08:10

Nirav


1 Answers

Add this permission in your menifest file

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
like image 127
Chandrashekhar Avatar answered Oct 28 '22 18:10

Chandrashekhar