Service, Rebind or not bound at all?

2019-07-25 10:30发布

问题:

Ok, so I have this problem. I have a service that plays a mp3 file. I can also pause and stop the mp3 by AIDL functions. That works perfect. I can press the homebutton an then restart the activity and I have still control over the service.

However, if I press the back key and then opens the activity I can't control the service anymore.

I think it should be possible to rebind to the control when restarting the activiy. But what's happening is that new instance of the service is started and I can't stop the mp3 that is playing. I can play a new mp3 however. What am I doing wrong?

回答1:

I think I understand what you want to do. After starting the activity and starting the mp3 playback, you exit from the activity. Then next time you open it you want to bind to the existing service and not start another instance of the service.

To achieve this, you have to use a started activity, and then bind to it. That is, use startActivity(Intent) on press of button, then in the next line, do bindService(Intent, connection, Context.FLAG_AUTO_CREATE);. This binds the service, then you can unbind in onDestroy (recommended) or onPause. Also, as this is a started service, it does not get destroyed when unbind. You have to explicitly call stopService(Intent) or stopSelf() to stop this service.

In the onResume of your activity, you should check if service is running, and then bind to it again using the code: bindService(Intent, connection, 0);

The connection object is a registered ServiceConnection that is fired everytime you bind or unbind to the service.

private ServiceConnection connection = new ServiceConnection() {

public void onServiceConnected(ComponentName className,
        IBinder service) {
}

public void onServiceDisconnected(ComponentName className) {
}

};

Also, you can send and receive messages from the activity, in your case the time of mp3 song play, using Messenger Service.

Refer to this link to learn more about using Messenger.

Hope this helps.



回答2:

I think is should be possible to rebind to the control when restarting the activiy.

I am sincerely hoping you unbound from the service, since you should not go through onDestroy() of an activity without unbinding.

But what's happening is that new instance of the service is started and I can't stop the mp3 that is playing.

This suggests that you did indeed unbind from the service, but the service did not stop the music playback when it was destroyed.

What am I doing wrong?

Most music players are designed to allow playback after the user exits the activity. Hence, music players should not be using the binding pattern, because the binding pattern does not support the service continuing to run after the user exits the activity.

Instead, use the command pattern: call startService() to start up music playback, perhaps with Intent extras to control what gets played. Call stopService() to stop music playback. Here is a sample project that demonstrates this, with the actual music playback stubbed out (as I cover that in a different book).



回答3:

This is quite an old question but I'm currently working with a related problem. What I've found is that Binding to a service is meant so other applications can also bind to your service and make use of its functionality.

That is an alright pattern, it might work for your application.

What I think you should do, since I cannot find a way to rebind to a service; is to check if the service is running (which could be accomplished easily through a static boolean in the service class that is changed to true when running and changed back to false when destroyed) and use that boolean to prevent the onCreate of your application to reinitiate a new service intent. You can then bind to it again and use its functions.

Also, if there's only a possibility of using 1 song at the time, you might want to change all these variables to statics so any bound application can influence its variables (think volume, etc.) and read from its variables as well (think time playing, title, etc.)

Hope this helps a bit!