title: Android Service - issue with BindService when use it along with startService
body: <p>I am newbie to android platform. Currently working on streaming radio application.</p>
  
  <p>I want to make the Application, starts the audio playback and continuos to play even application goes to background.</p>
  
  <p>I have created a Service using <strong>startService()</strong> so that play back will continue even though application goes to background. I have also used <strong>bindService()</strong> to get a serviceConnection reference to perform RPC calls with service.</p>
  
  <p>Application starts the service and playback started correctly and bindService able to get the reference and able to make RPC calls, everything works fine till app goes to background.</p>
  
  <p>I am calling <strong>bindService()</strong> and <strong>unBindService()</strong> call in <strong>onResume()</strong> and <strong>onPause()</strong> of main activity.</p>
  
  <blockquote>
    <p>When app goes to background, playback continues with out any issue. <strong>unBindservice()</strong> call detach the service connection and <strong>onUnbind(Intent)</strong> method got invoked in Service class. No issue so far.</p>
    
    <p>when application comes to foreground(while service is running and playback continues),
    I am not getting the service connection reference (<strong>onServiceConnected()</strong> is not getting invoked), even though <strong>bindService()</strong> is called in activity <strong>onResume()</strong> method. i have also verified Service class <strong>onBind(Intent)</strong> method is also not getting invoked.</p>
  </blockquote>
  
  <p>I am struggling with this issue for past few days. </p>
  
  <p>What is the recommended approach to use the <strong>startService()</strong> and <strong>bindService()</strong> with user defined Android Service.</p>
  
  <p>Any help to resolve this issue, highly appreciated.</p>
  
  <p>Activity onResume/onPause Code.
  </p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      Log.d(TAG, "onCreate =&gt; " + getIntent());
      super.onCreate(savedInstanceState);
  
      // set the default network preference, if it is not already set
      setDefaultNetwork();
  
      // initialize the navigation tab bars
      initializeTabBar();
  
      mPlayerController = AudioPlayerController.getInstance(this);
  
      mActionBar.setDisplayShowHomeEnabled(true);
      mActionBar.setDisplayShowTitleEnabled(true);
  
      mRemoteControlReceiver = new RemoteControlReceiver();
      IntentFilter filter = new IntentFilter(
              "android.intent.action.MEDIA_BUTTON");
      registerReceiver(mRemoteControlReceiver, filter);
  
      setVolumeControlStream(AudioManager.STREAM_MUSIC);
  }
  
  @Override
  public void onResume() {
      super.onResume();
      Log.d(TAG,"onResume");
      mPlayerController.bindService();
      restoreAppState();
  }
  
  @Override
  public void onPause() {
      super.onPause();
      Log.d(TAG,"onPause");
      mPlayerController.unBindService();
      storeAppState();
  }
  </code></pre>
  
  <p>Controller class has service related start/stop/bind/unbind methods</p>
  
  <pre class="lang-java prettyprint-override"><code>    public void bindService() {
      Intent intent = new Intent();
      intent.setClassName("com.vikkrithik.radio.indradio",
              "com.vikkrithik.radio.indradio.AudioPlayerService");
      Log.d(TAG, "bindService Context " + mPlayerContext
              + " serviceConnection =&gt; " + audioServiceConnection);
      mPlayerContext.bindService(intent, audioServiceConnection, 0);
  }
  
  public void unBindService() {
      mPlayerContext.unbindService(audioServiceConnection);
      Log.d(TAG, "unBindService done context =&gt; " + mPlayerContext
              + " serviceConnection =&gt; " + audioServiceConnection);
  }
  
  private ServiceConnection audioServiceConnection = new ServiceConnection() {
      public void onServiceConnected(ComponentName name, IBinder service) {
          Log.d(TAG, "onServiceConnected " + name);
          mAudioService = AudioPlayerServiceInterface.Stub
                  .asInterface(service);
      }
  
      public void onServiceDisconnected(ComponentName name) {
          Log.d(TAG, "onServiceDisconnected " + name);
          mAudioService = null;
      }
  };
  /**
   * Start the AudioService
   * 
   */
  public void startAudioService(Station station) {
      Intent intent = new Intent();
      intent.setClassName("com.vikkrithik.radio.indradio",
              "com.vikkrithik.radio.indradio.AudioPlayerService");
  
      Bundle extras = new Bundle();
      extras.putString("station_url", station.getUrl());
      extras.putString("station_name", station.getStationName());
      intent.putExtras(extras);
      mCurrentStation = station;
      mPlayerContext.startService(intent);
      bindService();
      Log.d(TAG, "startService called");
  }
  
  public void stopAudioService() {
      //
      // Check if AudioService is already created
      //
      if (null == mAudioService)
          return;
  
      Intent intent = new Intent();
      intent.setClassName("com.vikkrithik.radio.indradio",
              "com.vikkrithik.radio.indradio.AudioPlayerService");
      mPlayerContext.stopService(intent);
      Log.d(TAG, "stopAudioService done");
  }
  </code></pre>
  
  <p>Service Class Methods</p>
  
  <pre class="lang-java prettyprint-override"><code>@Override
  public IBinder onBind(Intent arg0) {
      Log.d(TAG, "onBind invoked");
      return audioServiceStub;
  }
  
  @Override
  public boolean onUnbind(Intent intent) {
      Log.d(TAG,"onUnbind invoked");
      return super.onUnbind(intent);
  }
  
  @Override
  public void onCreate() {
      super.onCreate();
      Log.d(TAG, "onCreate");
      audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  
      mNetworkStatusReceiver = new NetworkStatusReceiver();
      IntentFilter filter = new IntentFilter(
              ConnectivityManager.CONNECTIVITY_ACTION);
      registerReceiver(mNetworkStatusReceiver, filter);
  
      PreferenceManager.getDefaultSharedPreferences(this)
              .registerOnSharedPreferenceChangeListener(
                      mNetworkStatusReceiver);
  }
  
  @Override
  public void onDestroy() {
      Log.d(TAG, "onDestory");
      stopPlayback();
  
      PreferenceManager.getDefaultSharedPreferences(this)
              .unregisterOnSharedPreferenceChangeListener(
                      mNetworkStatusReceiver);
      unregisterReceiver(mNetworkStatusReceiver);
      cancelNotification();
      audioServiceStub = null;
      super.onDestroy();
  }
  </code></pre>
  
  <p><strong>Logcat output</strong></p>
  
  <blockquote>
    <p><strong>when app launches</strong></p>
  </blockquote>
  
  <p>TabbarMainActivity(15432): onResume
  AudioPlayerController(15432): bindService Context com.vikkrithik.radio.indradio.TabbarMainActivity@4150fdb8 serviceConnection => com.vikkrithik.radio.indradio.AudioPlayerController$1@418a2d30</p>
  
  <blockquote>
    <p><strong>When play button is pressed</strong></p>
  </blockquote>
  
  <p>AudioPlayerController(15432): bindService Context com.vikkrithik.radio.indradio.TabbarMainActivity@4150fdb8 serviceConnection => com.vikkrithik.radio.indradio.AudioPlayerController$1@418a2d30</p>
  
  <p>AudioPlayerController(15432): startService called</p>
  
  <p>AudioPlayerService(15432): onCreate</p>
  
  <p>AudioPlayerService(15432): onBind invoked <strong>onBind() invoked</strong></p>
  
  <p>AudioPlayerService(15432): onStartCommand with IndetntIntent { cmp=xxxx (has extras) }</p>
  
  <p>AudioPlayerController(15432): onServiceConnected   ComponentInfo{com.vikkrithik.radio.indradio/com.vikkrithik.radio.indradio.AudioPlayerService} <strong>onServiceConnected invoked</strong></p>
  
  <blockquote>
    <p><strong>when back button is pressed, app goes to background</strong></p>
  </blockquote>
  
  <p>TabbarMainActivity(15432): finish called</p>
  
  <p>TabbarMainActivity(15432): onPause</p>
  
  <p>AudioPlayerController(15432): unBindService done context => com.vikkrithik.radio.indradio.TabbarMainActivity@4150fdb8 serviceConnection => com.vikkrithik.radio.indradio.AudioPlayerController$1@418a2d30 <strong>onPause() calls unBindService()</strong></p>
  
  <p>AudioPlayerService(15432): onUnbind invoked   <strong>unBind Got invoked in Service</strong></p>
  
  <p>TabbarMainActivity(15432): onDestroy</p>
  
  <blockquote>
    <p><strong>App comes to forground</strong></p>
  </blockquote>
  
  <p>TabbarMainActivity(15432): onCreate => Intent { cmp=xxxx }</p>
  
  <p>TabbarMainActivity(15432): onResume  <strong>bindService is called from onResume</strong></p>
  
  <p>AudioPlayerController(15432): bindService Context com.vikkrithik.radio.indradio.TabbarMainActivity@4150fdb8 serviceConnection => com.vikkrithik.radio.indradio.AudioPlayerController$1@418a2d30</p>
  
  <blockquote>
    <p><strong>After playback is stopped</strong></p>
  </blockquote>
  
  <p>AudioPlayerService(15432): stop</p>
  
  <p>AudioPlayerService(15432): reset</p>
  
  <p>AudioPlayerController(15432): stopAudioService done</p>
  
  <p>AudioPlayerFragment(15432): removeVisualizer</p>
  
  <p>AudioPlayerService(15432): onDestory</p>