2

What I want to be able to do is toggle the visibility of an element in my activity FROM a service.

How can I pass the current activity to my service class in order to send back messages/call functions in my activity?

What happens if the user exits the app and the service is still running, but obviously without a running activity to call functions within?

Thank you.

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
  • possible duplicate of [android: notify activity from service](http://stackoverflow.com/questions/4111398/android-notify-activity-from-service) – David Snabel-Caunt Sep 01 '11 at 21:30

2 Answers2

4

I don't think it's a good idea. Here's a good answer about notifying an activity from a service: Notify activity from service.

Community
  • 1
  • 1
Michael
  • 53,859
  • 22
  • 133
  • 139
1

To answer your first question, you wouldn't be doing anything to the UI from your service. You have a few options for communicating between an activity and a service. You can set up a BroadcastReceiver. You can bind the service to your activity and use Android's AIDL to set up handlers for the communication. Plus others.

To answer your second question - make sure to stop your service in your onDestroy method. Alternately, if you've bound the service to an activity, the service will stop once the final activity that the service is bound to stops.

SBerg413
  • 14,515
  • 6
  • 62
  • 88
  • Ok, I think I'm very confused! I've created a service so that users can leave the app and the service will still run in the background. Users can go back to the app and stop the service themselves manually via buttons in the app. The service downloads information from the interwebs. So before and while its initializing I want a button which says "Start Service" and when its finished initalising and is in full swing (downloading the info) I want to send a message back to the activity to hide the button and show one for stopping the service. – Thomas Clayson Sep 01 '11 at 20:50