Binding to the same service instance in android
I'm playing around with the MediaPlayer
. I want the music to play when the user leaves the activity. However when I leave and return to the activity it looks like I'm not binding to the same instance.
This is my code:
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() {
开发者_Python百科 // 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;
}
}
}
It works just fine when I don't leave the activity, but when I do and the music is still playing, when I return and click stop, nothing happens. When I press the play button another stream is started.
The debugger shows that mpLoaded
is false, even through I can hear the service.
This is how I bind it.
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;
}
Are you unbinding from the service on a closing event handler such as onStop
of your Activity? If so, the service is unbound and destroyed, and the only reason you're still hearing the music is because the MediaPlayer is still active. With bound services, you get a new instance every time you bind and then unbind.
If you want a persistent service instance, use startService. The service's life cycle will be independent of any binding activities, and you can call stopService to end the service instance.
精彩评论