I'm trying to create an application that will only let you in a another activity if you plug in your headphones. I got a code, that works, but the problem is that it's checking the headphone is plugged in or not when the activity starts only. I want the application to always check the headphone state. Not only when the activity creates. How to do this?
AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if(audioManager.isWiredHeadsetOn()) {
Intent intent1= new Intent(this,Main2Activity.class);
startActivity(intent1);
}
Here's the code, it's fully functional, but how to make it to always check the state of the headphones if you in the activity? Because the app only checks the state of the headset when it's starts, so you have to plug in your headphone at first, and then start the application to make the new intent work.
Thanks for every answer.