title: Binding to Service Trouble
body: <p>Good Morning All,</p>
  
  <p>I am currently buidling a media player for Android, and I am having trouble binding my player service to an Activity and pulling data from it.  Bear with me...a lot of code follows.</p>
  
  <p>My code---</p>
  
  <p>Interface:</p>
  
  <pre><code>interface MyInterface{       
      void playFile( in String file);
      void shuffle ();
      String getPlayingData();
  
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>public class MyService extends Service {
  
  public Context context;
      public static String nowPlayingData = null;
  
  @Override
  public IBinder onBind(Intent intent) {
      return mBinder;
  }
  
  private final MyInterface.Stub mBinder = new MyInterface.Stub() {
  
      public void playFile(String file) throws DeadObjectException {          
         //Song playing method working great!
      }
  
      public void shuffle()throws DeadObjectException {       
          //This method picks a random song and passes it to nowPlayingData string                
  }
  
      public String getPlayingData() throws RemoteException {
  
          return nowPlayingData;
      }
  };
  
  @Override
  public void onCreate() {
      //Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
      Log.d(TAG, "onCreate");
      mp.setLooping(false); // Set looping
      }
  
  @Override
  public void onDestroy() {
      //Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
      Log.d(TAG, "onDestroy");
      mp.stop();
  }
  
  @Override
  public void onStart(Intent intent, int startid) {
      //Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
      Log.d(TAG, "onStart");
  
  }
  
  }
  </code></pre>
  
  <p>Main Screen Activity, Shuffle Button:</p>
  
  <p>Service is started in onCreate.  When I click on the shuffle button it uses a method in the service to play a random song, thus setting the nowPlayingData string.</p>
  
  <pre><code>public class mainMenu extends Activity {
  
  
  private MyInterface mInterface;
      private ServiceToken mToken;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.main);
  
      Intent startedSvc = new Intent(mainMenu.this, MyService.class);
      boolean success = this.bindService(
              startedSvc, svcConn, Context.BIND_AUTO_CREATE);      
      this.startService(startedSvc);
  
      findViewById(R.id.shuffle).setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
  
  
                  try {
          sInterface.shuffle();
          } catch (RemoteException e) {
          e.printStackTrace();
          }
          Intent play_screen = new Intent(MyPlayer.getContext(), NowPlaying.class);                   
              startActivity(play_screen);
  
              }
  </code></pre>
  
  <p>Player Activity:</p>
  
  <p>I want to bind to the service, and pull nowPlayingData over using the interfaces getPlayingData method.</p>
  
  <pre><code>public class NowPlaying extends Activity implements OnClickListener {
  
  public static String nowPlayingData = null;
  MyInterface mInterface;
  boolean isConnected = false;
  RemoteServiceConnection conn = null;
  
  @Override
  public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.now_playing);
      setVolumeControlStream(AudioManager.STREAM_MUSIC);
      Log.i("Log", "Player Loaded");
  
      bindService();
      getData();
      fillView();             
  }   
  
  
  public void fillView(){      
      //This method needs the nowPlayingData string to update the view                
  }
  
  
  
  class RemoteServiceConnection implements ServiceConnection {
        public void onServiceConnected(ComponentName className, 
       IBinder boundService ) {
  mInterface = MyInterface.Stub.asInterface((IBinder)boundService);
  isConnected = true;
              Log.d("Now Playing", "Service Connected" );             
        }
  
        public void onServiceDisconnected(ComponentName className) {
              sInterface = null;
         // updateServiceStatus();
              isConnected = false;
          Log.d( getClass().getSimpleName(), "onServiceDisconnected" );
          getData();
        }
  };
  
  
  
  private void bindService() {
  
  if(conn == null) {
      conn = new RemoteServiceConnection();
      Intent i = new Intent();
      i.setClassName("com.musicplayer.MyPlayer", "com.musicplayer.MyPlayer.MyService");
      bindService(i, conn, Context.BIND_AUTO_CREATE);
      Log.d( getClass().getSimpleName(), "bindService()" );
   } else {
     Toast.makeText(NowPlaying.this, "Cannot bind - service already bound", Toast.LENGTH_SHORT).show();
   }
  }
  
  private void getData() {
   if(conn == null) {
       Log.i("getData", "No Connection");
   } else {
      try {
         String data = mInterface.getPlayingData();  
         Log.i("Data Recieved", data);
         Log.d( getClass().getSimpleName(), "invokeService()" );
      } catch (RemoteException re) {
         Log.e( getClass().getSimpleName(), "RemoteException" );
      }
    }
   }   
  
  }
  </code></pre>
  
  <p>I have been studying many examples online, and perhaps my mish-mash of attempts has caused this to not work.  My code works all the way through to binding the service, but OnServiceConnected is never called, and conn remains null.</p>
  
  <p>Any help you guys can provide is greatly appreciated.</p>
  
  <p>Thanks,
  Josh</p>