3

I have looked for a solution, but getting solutions for SharedPreferences and SQLite databases. I have to store a single URL that my Android application will access for getting some data. I like to keep that as configurable, as in case I can change the URL and use the app with another set of data.

If I use strings.xml, will I need to repeat the same in case I am adding support for other languages?

For storing in SharedPreferences, I need to store it somewhere till the app is installed to be able to save it there.

Using an SQLite database for this seems too far fetched?

I think the basic thing would be to store it in a Constants class, but I am not sure whether that is the correct approach.

Can someone suggest a better solution?

midhunhk
  • 5,560
  • 7
  • 52
  • 83

3 Answers3

1

In our app we store all static configuration in the AndroidManifest.xml as meta-data properties.

The link to the docs can explain it better than I can:

http://developer.android.com/guide/topics/manifest/meta-data-element.html

Basically:

<meta-data android:name="api.url" android:value="http://api.some-api.com"/>

Then assuming the meta-data is at application level, you can get it using:

        Bundle data = context.getPackageManager().getApplicationInfo(
            context.getPackageName(),
            PackageManager.GET_META_DATA).metaData;

        String apiUrl = data.getString("api.url");
Ian Warwick
  • 4,774
  • 3
  • 27
  • 27
  • 1
    If this is something you want to keep private don't follow this method since it's accessible for all the other apps, for more info: https://stackoverflow.com/a/10352713 – moxi Oct 18 '17 at 05:10
1

You don't indicate that the url should remain private. So, you may be overlooking the obvious. Why not simply create a "configuration" file that is installed with the application? This can be achieved by following the methodology here. Once it is installed, your application can simple read it and process accordingly.

Community
  • 1
  • 1
rrirower
  • 4,338
  • 4
  • 27
  • 45
  • Yes, the URL need not be private. All I want is to keep such language independent static configuration data at one place and not inside any Constants class. – midhunhk Oct 09 '13 at 05:03
0

I would store the URL in Constants. unless you need to change it between releases. If you want to store it in XML, then put it into donottranslate.xml, so that Lint doesn't complain.

injecteer
  • 20,038
  • 4
  • 45
  • 89