I’ve checked the suggested solutions and can’t find my answer. If the answer is out there, then I’m sorry for posting it again.
I’m consuming a wcf rest service.
In my test activity I do;
private final static String SERVICE_URI = "http://10.0.2.2/Service1.svc";
This will eventually be used in various activities.
So what I want to do is: private final static String SERVICE_URI = [CONSTANT]
so that if I need to re-point the service somewhere else, a single code update will result in dependent activities pointing to the correct location, allowing them to work, rather than having to update each activity.
So: how / where would I create such a constant in Android, and how would I reference it?
Many thanks any help.
From what you describe it sounds like you need to look at String Resources.
You can access them from any component in your Android app at any time and they’re effectively ‘constant’ at build time.
EDIT Just to expand on the use of resources…
…take a look at Dororo’s answer. All resources (strings, images / drawables and even UI elements such as Buttons etc) are accessed using ‘resource IDs’. Any resId, i.e.
R.blahis an int representing the resource. As such, it needs to be fetched in the correct way.With UI elements we use
findViewById(...)and with strings we usegetString(...)as in Dororo’s example.My point was that the strings are constant at build time so it isn’t necessary to declare any variable as
final staticthat represents a string resource as part of any class such as anActivitybecause you can access any string using theContext.getString(...)method as shown in the link.