Issue
My Problem
I have a Service within which I monitor location changes. Its vital this stays running when in the background so it is a Foreground service.
My problem is whilst its in the background and things are getting destroyed, how do I save this data.
My Question
This brings me onto my potential solution.
I have a subclass of Application
which I declare in my manifest file.
Would it be safe to see that any objects stored in this Singleton
would be accessible throughout my Service
's life and therefore give me a means to save my locations?
Thanks in advance
Solution
The short answer to your question is yes, Application
basically lives as long as your process does so anything that you store there is as stable as it can be in memory until such time that Android may need to reclaim your app from the background to get more memory for a foreground application. However, not much benefit is added over simply saving your state data inside the Service
itself. Android does not have a mechanism by which components out of an application are selectively destroyed to reclaim memory. If it needs additional memory to keep the foreground process happy, it simply kills the entire process of other apps in order of priority (based on recent use and their foreground/background priority state).
By having a running Service
in your process, your application has already promoted its priority to be more important than other apps in the system that are in the background, reducing the likelihood that your process gets killed. By making your Service
run as foreground, you've only slightly increased this priority level further and it's not the sort of thing Google recommends you do for long periods of time. When the system comes under enough memory pressure that apps start getting killed, eventually your process will suffer the same fate.
If you truly need any of your data to live beyond the life of your process, you need to persist it into any of Android's local storage formats.
Answered By - devunwired
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.