Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call service methods from activity without binding

Tags:

android

aidl

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?

like image 745
ᴘᴀɴᴀʏɪᴏᴛɪs Avatar asked Oct 28 '25 09:10

ᴘᴀɴᴀʏɪᴏᴛɪs


1 Answers

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.

like image 71
Blackbelt Avatar answered Oct 31 '25 12:10

Blackbelt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!