Access Running service and call its method?
Asked Answered
C

2

8

I've created a service that sends location in back ground now

i want to create a method in my ACTIVITY which will access the currently running instance of service and will call its method

Say Location Service having Methode

SendLocation()
{
/// to do 
}

Now in Activity A:

Service s = getCurrentRunningServiceInstance // something like this

and call its method

s.SendLocation();
Canalize answered 3/7, 2012 at 6:38 Comment(0)
S
4

You can't call a method on a Service directly. What you need to do is bind to the Service.

In your Service, override onBind() and have it return a valid IBinder then use this in a client/server fashion.

See the docs for Services particularly with respect to Creating a Bound Service and also the docs for Bound Services.

Setaceous answered 3/7, 2012 at 6:46 Comment(0)
S
1

Method calling of service is not recommended, Instead of this, follow below steps

1) create an BroadcastReciever in Activity.

2) from service when location changed, send a broadcast intent with location extras,

3) listen it from Receiver in Activity and update activiy accordingly.

Studhorse answered 3/7, 2012 at 6:43 Comment(1)
hi,it's possible to call method of any running service?Bilbo

© 2022 - 2024 — McMap. All rights reserved.