Hunt
Hunt

Reputation: 8435

send messages to service with a call to startService

I've create a service and I'm accessing it via my activity using bindService function but I wanted to keep running my service even after my activity exited.

And for that I've to use startService but previously I was using bindService with a ServiceConnection where I'm sending message to the handler (in a service) once the onServiceConnected gets called.

But I don't know how can I send message with a startService as I don't have any place like onServiceConnected to send the message to the handler.

note: I want to keep running my service even I'm out of my application as I'm updating widget from my service.

so a service has two roles

  1. update textview inside activity
  2. once the activity exits , service will update widget

Upvotes: 2

Views: 1426

Answers (2)

Luis
Luis

Reputation: 12058

You have 3 option to communicate with your service:

  • Put an extra to the intent and call startService. You can call it several times, even if the service is already running
  • Create a Messenger and use it to communicate with the service
  • Use bindService

Be sure to use START_STICKY in your service so it keeps running after yhe activity finishes, and startForeground() to show a notification or your service will be killed by SO after some time (in average, 30 minutes to 1 hour).

Starting flow:

  • ClientActivity (CA) starts the service and sends the CA Messenger address as an extra in the intent to the service.
  • On service start it sends the service Messenger address back to the CA (using the CA Messenger address to send the message).
  • Finally, service returns START_STICKY.

With this both, client and server will know each other Messenger addresses and no bind is required.

To stop service:

To stop the service use the follwoing in your activity:

stopService(new Intent(ActivityClass.this, ServiceClass.class)); 

Reconnection:

If you leave the clieant activity (CA) and want to reconnect to service when app is restarted, you have two options:

-Call startService again from your CA. This will call again the onStartCommand in the service. The service onCreate() is called only if the service is not yet running. You can use the starting flow described above to get the service Messenger;

-You can make the service Messenger public and static public static Messenger mMsgService = null;. So, when your CA starts, it can check whether service messenger has been initialized (not null) and use it to communicate with the service. When setvice stops, need to set mMsgService = null;

good luck.

Upvotes: 4

user936414
user936414

Reputation: 7634

There is no other way to communicate to a Service other than binding to that service. Binding the service in an activity doesn't necessarity mean that the Service will be(or need to be) unbinded onDestroy of the service. If you don't call the unbindService method, then the binded Service will be running always. So the solution is bind to the Service and don't call unbindService onDestroy of the activity.

Upvotes: 0

Related Questions