burakk
burakk

Reputation: 1291

How to Access the Instance of the Service from an Activity Other Than the Activity Starting the Service

I have a service that is being started in Activity B using:

private Intent streamPlayerServiceIntent;
.
.
.
streamPlayerServiceIntent = new Intent(getSherlockActivity(), StreamPlayerService.class);
streamPlayerServiceIntent.putExtra("url", result);
getSherlockActivity().startService(streamPlayerServiceIntent);

and I would like to access the service instance from Activity A. How can I access the service, which method would be the most suitable way? The service plays a stream from an URL.

I don't want access the service by declaring the intent as static or convert service to be a singleton since I intent to create another instance of the service later.

Upvotes: 1

Views: 2422

Answers (1)

Merlevede
Merlevede

Reputation: 8170

What you need to do is bind to the service. It's a little long to explain here (and I would probably end up pasting the manual).

In short, when you bind to the service you provide a callback connection object. This connection object (probably inside your activity) gets notified (similarly to a control's onclickListener) when the binding has been successful and you can retrieve a pointer to the service's instance. Once with a pointer to the service in your hands you can do whatever you want.

Check the documentation, there's a very good example.

Upvotes: 1

Related Questions