I'm just starting to write a new android app and wanted to have a clean design.
I want some variables to be initiated with a default value. Those default values should all be in one file called settings (or something similar). However the user should be able to change some of those values and save them.
Changing user settings is normally done in shared preferences files. However I did not found out how I can pre-populate them with my default values.
Is there any other way how I can have some sort of a settings file that already has values in it? I don't want to hardcode values in my java application as I want to have a single file to adjust when I need to change the default values.
Secondary goal: I would like if the user would not lose his saved values when the app updates.
Can anyone give me some hints where to search?
Related
I have a property file which has a field date, which is used in jsp logic. I need to have an option to modify those dates and get reflected in jsp without server restart.
For example - I need to block content of jsp page on suppose 3 dates and I want to have flexibility to modify.
I have tried hard coding the dates in properties file and it works but the change in property file doea not reflects without server restart which is not preferrable, any advice would be appreciated
This might be overkill in your situation, but since the dates seem to change unrelated to the deployment/maintenance cycle of the system you could very well consider to treat it like normal data in a database table.
Apart from the big guns (storing data in a database) as #midin suggested, this is what comes to mind:
store values in a jsp and include that. As this would be recompiled on the fly your new values would be active after save.
store values in external (from your webapp) file and re-read every so often
use a Preferences object in the app-servers context
if you have a database connected to your app already: use that.
Say I have the following
Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
String ID1 = "Test1";
System.out.println(prefs.getBoolean(ID1, true));
prefs.putBoolean(ID1, false);
//prefs.remove(ID1);
Is this variable persistent the next time I execute my program?
Where do these variables store?
What is the proper way of utilizing this?
Is the approach better than using properties files?
Yes, the value is persistent but only for the user. It won't be there for other users.
This is OS specific. For Windows it uses the registry, for Linux I believe it uses hidden files in the user root, although I'm not 100% sure.
You've got a pretty good example in your question.
It is different, not better. Preferences are a way of transparently storing settings for an application. These settings may be updated in run time by a user (for example you could use prefs to store user specific settings). Preferences are not meant to be editable outside of the application. Properties files tend to store hard setting specific to an application. These settings are the same for each user and tend not to change often. Properties files are text files and tend to accompany an application on deployment. You can edit them easily using a text editor. It is fairly rare for an application to update properties files.
I have one question about Android. I need to run one of my activities only once - at the beggining. So, usually the best solution is to create file which contains flag isFirstRun and check the value after application's start.
But in my application it is very important to protect this file before deleting by user. Even if user has rooted phone he should not be able to change the value or delete this file.
So, is it possible to write this information to any Android system registry or somewhere else where user can't change this value?
No, it is not possible, for a simple reason, a root user have access to everything by definition. It won't make sense to have a program that has more rights than root.
The user can delete all the data your application saves. Consider saving this information on some server.
I'm working on a live wallpaper that is configured using a preferences screen. I use shared preferences to store things like color, speed and movement of the wallpaper. As I've got a lot of settings, I'd like users to be able to:
save all the current preferences under a profile, where the profile is assign a name by the user.
the user can then select a profile by name from a list and have all their settings restored.
the user can also delete profiles.
Can anyone recommend a nice way of doing this?
One idea I had was to save all the current preferences to an XML file and selecting a profile would just load the file and set the shared preferences based on this. However, if I had 20 or so profiles, I would need to inspect all the files to produce a list of profile names (as I'd have to store the profile name in each file) which seem inefficient.
I implemented a profile system in one of my apps with the help of an SQLiteDatabase (basics are explained in the notepad tutorial). You only need one table to store profile name/id, and all of the settings you want.
save each profile as a new record in your table, with an appropriate name field
retrieve all records and display the list of names when you want to user to be able to select a profile (e.g. SELECT * FROM Profile)
delete a profile in your system by deleting the corresponding record (e.g. DELETE FROM Profile WHERE name='john')
I also saved the name/identifier of the current profile using SharedPreferences so that my system had an easy way to know which profile was currently/most recently active, outside of the context of profile loading.
I have implemented an Java Swing application that uses an embedded JavaDB database. The database need to be stored somewhere and the database tables need to be created at the first run. What is the preferred way to do these procedures?
Should I always create the database in the local directory, and first check if the database file exist, and if it doesn't exist let the user create the tables (or at least show a message that the tables will be created).
Or should I let the user choose a path? but then I have to save the path somewhere. Should I save the path with Preferences.systemRoot();, and check if that variable is set on startup?
If the user choses a path and save it in the Preferences, can I get any problems with user permissions? or should it be safe wherever the user store the database? Or how do I handle this?
Any other suggestions for this procedure?
I would let the user choose. That way they can run multiple instances (simultaneously or otherwise). Supplying a sensible default would be a good move.
Store the path using the Preferences API (I would store per-user but that may differ depending on your application), and use the File object to determine if directories exist and/or are permissioned properly.