2011-04-20 68 views
2

中的相同服务实例我正在玩MediaPlayer。我希望音乐在用户离开活动时播放。但是,当我离开并返回到活动时,看起来我没有绑定到同一个实例。绑定到android

这是我的代码。

public class MusicService extends Service { 
     private NotificationManager mNM; 

     public class LocalBinder extends Binder { 
      MusicService getService() { 
       return MusicService.this; 
      } 
     } 

     @Override 
     public void onCreate() { 

     } 

     @Override 
     public int onStartCommand(Intent intent, int flags, int startId) { 
      return START_STICKY; 
     } 

     @Override 
     public void onDestroy() { 
      // Cancel the persistent notification. 

      // Tell the user we stopped. 
      Toast.makeText(this, "destroyed", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 
     public IBinder onBind(Intent intent) { 
      return mBinder; 
     } 

     // This is the object that receives interactions from clients. See 
     // RemoteService for a more complete example. 
     private final IBinder mBinder = new LocalBinder(); 

     public void showtoast(int i){ 
      Toast.makeText(this, "showtoast"+i, Toast.LENGTH_SHORT).show(); 
     } 

     //Music player functions 


     String path = "http://mp3stream"; 
     MediaPlayer mp; 
     Boolean mpLoaded=false; 
     public void play(){ 
      if(!mpLoaded){ 
       try { 
        mp=new MediaPlayer(); 
        mp.setDataSource(path); 
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
        mp.prepareAsync(); 
        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 

         public void onPrepared(MediaPlayer mp) { 
          mp.start(); 
         } 
        }); 
        mpLoaded=true; 
       } catch (IllegalArgumentException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
     public void pause(){ 
      if(mpLoaded){ 
       mp.stop(); 
       mpLoaded=false; 
      } 
     } 
} 

的时候,我不离开活动它工作得很好,但是当我做和音乐仍在播放,当我回来,然后点击停止,什么都不会发生。当我按下播放按钮时,另一个流被启动。

调试器显示mpLoaded是错误的,即使通过我可以听到的服务。

这就是我如何绑定它。

private MusicService mBoundService; 
    private ServiceConnection mConnection = new ServiceConnection() { 
     public void onServiceConnected(ComponentName className, IBinder service) { 
      mBoundService = ((MusicService.LocalBinder)service).getService(); 
      mIsBound=true; 
      Toast.makeText(Main.this, "service connected", Toast.LENGTH_SHORT).show(); 
     } 

     public void onServiceDisconnected(ComponentName className) { 
      mBoundService = null; 
      Toast.makeText(Main.this, "service disconnected", Toast.LENGTH_SHORT).show(); 
     } 
    }; 

    void doBindService() { 

     bindService(new Intent(getApplicationContext(), MusicService.class), mConnection, Context.BIND_AUTO_CREATE); 
     mIsBound = true; 
    } 

回答

3

你从服务解除绑定为您的活动onStop关闭事件处理程序等?如果是这样,该服务就会被解除绑定并被销毁,而您仍在听音乐的唯一原因是因为MediaPlayer仍处于活动状态。在绑定服务的情况下,每次绑定然后解除绑定时都会得到一个新实例。

如果您想要一个持久服务实例,请使用startService。该服务的生命周期将独立于任何绑定活动,您可以拨打stopService来结束服务实例。