Call service methods from activity without binding

1.7k Views Asked by At

I have a push service running for my application responsible for keeping the TCP connection alive and create notifications on incoming messages.

I need this service to run even if the activity is closed, so the solution posted online was to call startService() instead of bind(), and indeed this solves the problem of the service ending on activity destroyed.

However with no Binder how can I call methods of my service? One way I've managed to get to work is through BroadcastReceiver. Is there a simpler way to achieve such a behaviour?

1

There are 1 best solutions below

5
On BEST ANSWER

add an action you use to call startService, one for every different method/case you want to call/handle, and retrieve it with intent.getAction(), when onStartCommand is called

From the documentation of startService

Every call to this method will result in a corresponding call to the target service's onStartCommand(Intent, int, int) method, with the intent given here. This provides a convenient way to submit jobs to a service without having to bind and call on to its interface.