Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing static configuration data

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?

like image 231
midhunhk Avatar asked Jan 26 '26 03:01

midhunhk


2 Answers

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.

like image 118
rrirower Avatar answered Jan 28 '26 20:01

rrirower


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");
like image 38
Ian Warwick Avatar answered Jan 28 '26 18:01

Ian Warwick