1

I just want to check if the headphones are plugged in the Android device.

Then when I press a button, it should check whether the headphones are plugged in. If they are plugged in, then I would play an audio, otherwise I would display a toast to plug in the headphones, i.e. I would not do anything until the headhones are plugged in.

OMKAR KAMATE
  • 33
  • 1
  • 4
  • 4
    Possible duplicate of [Android: Checking if headphones are plugged in](http://stackoverflow.com/questions/2764733/android-checking-if-headphones-are-plugged-in) – nandsito Mar 17 '17 at 11:03

1 Answers1

5

You can use BroadcastReceiver

  public class MainActivity extends AppCompatActivity {
    private MusicIntentReceiver myReceiver;
  boolean  isHeadphoneConnected;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        MusicIntentReceiver myReceiver = new MusicIntentReceiver();
        IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        registerReceiver(myReceiver, filter);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            if(isHeadphoneConnected)
                Toast.makeText(MainActivity.this,"Headset is plug in",Toast.LENGTH_LONG).show();
                else
                Toast.makeText(MainActivity.this,"Headset is not plug in",Toast.LENGTH_LONG).show();
            }
        });
    }
    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:
                        isHeadphoneConnected = false;

                    break;
                    case 1:
                        isHeadphoneConnected = true;
                        break;
                }
            }
        }
    }
}
Expiredmind
  • 788
  • 1
  • 8
  • 29
  • I dont know how to use this class... I mean how could i use this in onclick event of my button – OMKAR KAMATE Mar 17 '17 at 12:11
  • Ive edit my answer. Put `MusicIntentReceiver` into your activity class and then create the instance of it from 'onCreate` method. Then whenever you clik on button you can check the `isHeadphoneConnected` flag – Expiredmind Mar 17 '17 at 12:39
  • My app is crashing when i did this.. Is there some permission to be declared in android.manfest file – OMKAR KAMATE Mar 17 '17 at 13:11
  • Nope, See my edit, Ive made a example application to show how can you done it. – Expiredmind Mar 17 '17 at 13:19
  • This Worked smoothly:-)...but Actually previously I selected broadcastreciever as a new activity like from file-> new->other-> broadcast Reciever........ is it not possible to do it this way – OMKAR KAMATE Mar 17 '17 at 13:31