I have some configuration I want to save it in my Android application and read it whenever I need , for instance, the server URL that it should try to access like that.
Is there any similar mechanism like web.config in ASP.NET available in Android?
A central configuration file that can be set up manually and then read by the application? Any help would be appreciated!
We use a .properties file in assets folder. It works out very well for us as we support multiple carriers with this, write to it (in case some values, sent from server, need to change. This is done at app start time, thus making our code configurable from server).
You can throw things like that into your strings.xml file. But, since you can't actually modify these values in real-time (since it's a distributed application rather than running on a server), throwing it into a constants class is quite acceptable.
Use Shared Preferences.
Here's a link Shared Preferences
You can use sq lite database files for it. You have a native API to read and write those and on top of that a command line tool.
If you want to create an XML file instead, then it's no different than any other xml file (unless you are thinking about the Shared Preferences, which use an xml format to save the data, but I believe it's not the best API for your application).
I was stumped on this too, but came across Managed Configurations in the Android documentation.
Managed configurations, previously known as application restrictions, allow the enterprise administrator to remotely specify settings for apps. This capability is particularly useful for enterprise-approved apps deployed to a managed profile.
It allows you to set a default value in case you rather not getting into the enterprise admistration business but leaves that option open for the future.
There is a caveat. This only works if your app is registered for EMM. Otherwise you will retrieve an empty map of restrictions.
Related
I'm developping an app that contains some settings like server ip, sharedpreference filename and other that I would prefer save into a single file and not hard coded somewhere in the code.
So I would like to know what are the best practices nowadays concerning the deployement of these kind of settings. More, how to access it everywhere in the app ?
The android sharedpreference allows me to save it after the 1st launch but I don't know how to deploy it with the App.
regards,
A common way to deploy arbitrary data is the use of raw resources.
Create a file with your settings and save it to the raw resource folder. When your app is run for the first time, you can create your SharedPreference instance and copy the values from the raw resource to the preferences.
One option is to create a Configuration.java class and put the configuration there. You can have different build flavors for different build types (development, production etc.)
Another option would be to use the class generated by Gradle: BuildConfig.class, where you can put some configuration data from your build.gradle script.
EDIT:
Shared Preferences shouldn't be used for that if the server IP doesn't change during the lifetime of the application.
As the title says - is there something in iOS that lets you say 'this file is allowed to be accessed by other apps' - preferably only readable, but read/write would be good enough.
Basically I am trying to place a file outside the sandbox so other apps can read it - preferably via the openURL that points to a local file rather than an http address.
Thanks
Edit:
I just received an answer from Apple Tech support and they told me that this is currently impossible (just after iOS 6 released)
I don't think it's possible to do it the way you describe. Last I checked, apps can only write inside their sandbox dir and cannot read inside other apps' sandboxes (though IIRC there used to be a few other writable directories).
What, exactly, is the end result you are trying to achieve?
If you know the app you want to read the data, you can use a custom URL scheme.
If you have a file of a certain type (extension/content-type, I think), you can use UIDocumentInteractionController to let the user pick an app to open it in.
If you just want to share some data with any app, the closest I can think of is a custom UIPasteboard. OpenUDID does this for a slightly dubious purpose.
I am new to Android development using eclipse, although not new to software development in general.
For my first real project, I am trying to modify the example SoftKeyboard that is supplied with the SDK. I want to modify one of the keys to act as a function key, when followed by a single letter key it will enter a canned string - performing a macro function.
So far so good. I have the key and graphics modified, and found where to respond. I would like to put the canned strings in an editable Properties file stored where the keyboard can find them.
That is where I'm having trouble. It seems that I can't to create and save a file. I don't know if it's read/write permission problem, whether the keyboard (it runs as a service) is not allowed to create a file, or my code is just plain wrong.
Can someone help me out – point me in the right direction?
Thank you very much.
Barry.
If these are canned files that come with the APK you install to the device and only need to read (not write), you can place them in the assets folder of your project. Then use the resource manager to load them:
Resources resources = getResources();
InputStream moduleSearchTemplateIn = resources.getAssets().open("file/name/here.properties");
If you want to read/write files on the SD card, you'll need to add a permission to your manifest. Though, for this purpose, I'd probably prefer a SQLite table.
I'm currently writing an android app, that interacts with an external server.
I want to make sure that during the development, Android makes connection to a test server instance, but the application should make connection to a production server otherwise.
Currently, URL of the server is stored in one of the xml files - res/values/strings.xml as
<string name="server_url">XYZ.example.com</string>.
Right now, I'm just modifying the xml files in local machines depending on my situation.
What is the idiomatic way to handle case like this?
The XML files are pretty much it. An example would by the mapview api key.
To develop mapview apps you need a key based on your build certificate. Most folks build and test with a debug key and then deploy with a release key. You need to request a different api key for each build key. I keep both map api keys in my resource file, and then comment out the one I don't need when debugging.
An alternative would be to keep two strings in your strings file, and then use some programatic means of selecting which key to use. AdMob uses this type of approach, i.e. if you are testing, you add a test device to the add request. Since you don't want everyone in the world who uses the same phones as you to only see the test adds, you need to wrap the calls in a check for test versus release build. I control this with an integer resource value defined in an xml file. This ends up behaving similarly to a C/C++ DEBUG macro. I still have to change to value of the resource value, but I only have to do that in one place. Alternatively you could use comments to hide the test server: eg
String server = context.getString(R.string.realserver);
server = context.getString(R.string.testserver); //comment out this line prior to release
I am not a fan of comment/uncomment method because its to easy to forget, especially if you have multiple places where the resource is accessed.
If there is a better way to handle this, I too would be interested.
In the case of a windows application(EXE/DLL), we can change or add language resources within the binary without re-compiling it. Can the same be done in case of an Android application? Is there any editor available to make this happen?
My plan is to develop the application in English and then release it to the sales department, where they will be responsible for the localization of the application without compiling and packaging it into a new APK. I just want to split the development part and localization part of the app.
The correct way to localize is to create a string resource for your base language and then have that localized and reimported into your project for every language that you support.
Much more detail can be found in the Localization documentation.
I don't believe there is a safe/supported way to inject localized strings into your app after it's been built.
No. You can not, because once your apk is signed then modifying it after this (you can always do that as apk is just a zip file) will corrupt the signed binary.
When having multiple languages with your application you have to build them into the application itself. Android uses XML files to store strings used within your application. Android allows you to add language localization files containing local specific strings. You can't do this without recompiling your project so you'll want to do it as a future update or right from the start. But you can't have the marketing department do it, that's just not a good idea.
As others have said, the short answer is no. The long(er) answer is sort of. If you pack all your language resources into remote XML that can be updated from the web, then with a little bit of forethought you can do all sorts of live updates to your app's strings, graphics, etc.
So if you want to use the standard R.string method for everything it will be a little difficult. I think it's possible to do something funky with a dynamic classloader for the assets and static dex classes (basically classes of data with just inline byte arrays that can be decoded after). However that would still require compiling. See Custom Class Loading in Dalvik for more info.
Another approach would be more of a standard Java implementation. Java has a class known as ResourceBundle. You could create a ResourceBundle from a property file (key-value plain text, or even property xml). Then these files could be loaded outside the apk, via a network connection or sdcard or other file type resource and deleted as necessary. You will have to write the loader code for it, but that's going to happen with any solution. This solution will be less performant and outside the standard design methods for android but it will solve the problem being asked to solve. Like you won't be able to use R.string or #string/whatever for any of these resources but I think you may be able to write an adapter to such resources (like your own TextView extension and whatever that would allow all of this). It's a matter mostly of how much work you want to invest in solving this actual problem.
Honestly I would opt for trying to distribute whole apks with only the targeted language if you are trying to save space, but then there is no way to change locale for the app at runtime :(