In an Android utility class, I want to get a system preference value in a class, but I don't have the context there, because the class that calls it doesn't have the context either. I've found that for Resources one can use the static Resources.getSystem()
function. Is there any similar way for getting system preferences without context?
My class isn't an activity nor service. It's a utility class. Could give more info if needed.
To get a reference to the ApplicationContext in a Spring application, it can easily be achieved by implementing the ApplicationContextAware interface. Spring will automatically detect this interface and inject a reference to the ApplicationContext: view rawMyBeanImpl. java hosted by GitHub.
There are mainly two types of Context that are available in Android.
Definition. it's the context of current state of the application/object. It lets newly-created objects understand what has been going on. Typically, you call it to get information regarding another part of your program (activity and package/application).
You've got to send it a Context - don't try to run away from your responsibilities. :) Your utility class must be getting called by an Activity or Service at some level, and you're going to have pass that Context all the way down the line, through every method call. I know it's annoying, I've had to do similar things myself. Consider it an incentive to keep your code simple and to require as few method calls possible to get something accomplished.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With