global variable in app with service: use Application or SharedPreferences class?
My App uses one service in the background. To share data between the UI and Service I'm currently using IPC... but I wonder if it's possible to use the Application class or the SharedPreferences class to easily share global variables between the service and the Activities.
I found example code on how to use the Application class to create global variables: http://www.helloandroid.com/tutorials/maintaining-global-application-state
Here I found an example to use SharedPreferences: http://marakana.com/forums/android/examples/63.h开发者_如何学Pythontml
which one is better for sharing data between an activity and service ?
For the duration of time that your app is in memory, storing state in a globally accessible object is a lot less overhead than the SharedPreferences. You can easily use SharedPreferences to serialize your app state so it can be reloaded later, but I would say to think of SharedPreferences as a storage mechanism and use a derived Application class for stuff you will be reading and writing to often while your app is running.
精彩评论