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();

2 Answers2

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.

Squonk
  • 48,735
  • 19
  • 103
  • 135
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.

AAnkit
  • 27,299
  • 12
  • 60
  • 71