Pass current activity to a service so that the service can manipulate the view
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 happ开发者_如何学Cens if the user exits the app and the service is still running, but obviously without a running activity to call functions within?
Thank you.
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.
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.
精彩评论