2011-04-20 67 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來結束服務實例。