kenny
kenny

Reputation: 1105

Is a static variable in an apk available to a background service?

I am using a helper class for a couple of static flags for my app.

Would a background service started from my app have access to those flags in case my app is not running?

EDIT: When I am testing it, it seems to work, I would just like to know if it is a recommended thing to do. (Or should I use a singleton or the strings.xml file? Do these persist?)

Upvotes: 1

Views: 189

Answers (1)

Shobhit Puri
Shobhit Puri

Reputation: 26017

AFAIK yes it would be available. If you have few constants that remain same overtime, you can create a Constants class and put your static values there.

public class Constants {
    public final static String MY_CONSTANT = "VALUE";
    // ... all the other constants 
}

You can access these constants from any class within the app and for using it from your service just do Constants.MY_CONSTANT.

Edit:

When I am testing it, it seems to work, I would just like to know if it is a recommended thing to do. (Or should I use a singleton or the strings.xml file? Do these persist?)

Yes its a recommended thing to do. Check answers here Android intent Filters: better as constant or in String.xml

Upvotes: 2

Related Questions