title: How to check and redirect audio between wired headset and speaker phone?
body: <p>I am developing a simple app , that should <em>toggle</em> between speaker phone and wired headset to play audio on a button click event. I am trying to make use of <em>isWiredHeadsetOn()</em> function, but it says that this is deprecated for Android API lvl 5 onwards. So how do I check if currently audio is playing or not through wired headset so that I can redirect it to the phone speaker?</p>
  
  <p><strong>Note:</strong> I start my app with the headphone plugged to the 3.5mm jack of the phone.</p>
  
  <p>This is my attempt at the code so far:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_audio_re_direct);
      redirect = (Button)findViewById(R.id.redirect);
      final AudioManager audio =(AudioManager)getApplicationContext().getSystemService(AUDIO_SERVICE);
  
      redirect.setOnClickListener(new View.OnClickListener() {
  
          @SuppressWarnings("deprecation")
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              if(audio.isWiredHeadsetOn())
              {
                  audio.setWiredHeadsetOn(false);
                  audio.setSpeakerphoneOn(true);
                  Toast.makeText(getApplicationContext(), "SpeakerPhone On", Toast.LENGTH_LONG).show();
                  redirect.setText("Turn on headset");
  
              }
              else 
              {
                  audio.setSpeakerphoneOn(false);
                  audio.setWiredHeadsetOn(true);
                  Toast.makeText(getApplicationContext(), "Wired Headset On", Toast.LENGTH_LONG).show();
                  redirect.setText("Turn off headset");
              }
  
              }
  
      });
          }
  </code></pre>
  
  <p>But the app is not toggling at all. Initially it detects that the wired headset is present, shows the Toast message <em>SpeakerPhone On</em> and that's it. It does not toggle between the two.</p>
  
  <p>Someone please help me to make this work. Thanks.</p>