I am using the Preference-API..
Typically when I need to retrieve the value of a Preference, I currently do something like this:
final SharedPreferences getPrefs =
PreferenceManager.getDefaultSharedPreferences(getActivity());
boolean isThisPrefEnabled = getPrefs.getBoolean(REFERENCE_TO_PREF_NAME, false);
// OR
String theChosenPref = getPrefs.getString(PREF_NAME, DEFAULT_VALUE);
But I'm curious, couldn't I also do it like this? and if so, what is the difference?
Preference nameOfPref = findPreference(PREFERENCE_KEY);
boolean isPrefEnabled = nameOfPref.isEnabled();
// OR
String thePrefValue = nameOfPref.toString();
It seems to be more efficient, but the first example seems to be what get's used. Why is this?
Thanks.
SharedPreferences is an android specific interface documentation
android.content.SharedPreferences : is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. This makes reading the data very easy. But as easy as it is to store a small amount of data as difficult it is to store and read large structured data as you need to define key for every single data, furthermore you cannot really search within the data except you have a certain concept for naming the keys.
Preferences is a core java class documentation
java.util.prefs.Preferences : This class allows applications to store
and retrieve user and system preference and configuration data. This
data is stored persistently in an implementation-dependent backing
store.
Related
What I thought was going to be the easiest solution, is proving to be the most confusing.
I am creating an app that needs to reference information from multiple "accounts"... such as name, email address, acct number, password and a few other fields... approximately 15 items. There can be as many as 15-20 of these accounts in this app depending on the user. Each one will be manually added and the information filled in and stored.
I decided to use a Preference Fragment to make entering this information simpler because everyone is familiar with how Android Preference screens work and the information being entered is pretty straightforward. Why create my own input screens when the built-in looks like it will work.
I asked on here opinions if I should store the data in the preference or an SQL database. Basic consensus, depends on if I need to search or sort, might be easier in an SQL database.... otherwise, internally in preferences.
I created the code for the SQL database and then had some confusion on how to get the data in and out of the preference code. I asked on here for some help in getting this to work. Unfortunately, that sort of got lost and was again recommended that I store in pref instead.
So, I started looking into keeping it in pref files, again.
Because there are approximately 15 fields per account, and the possibility of 15 or more accounts, having a single preference XML file with 225+ entries (name1, name2, name3... email1, email2, email3... etc) seems the wrong way to do that.
If I don't do it that way, I would assume I need to create a separate XML file for each "account" ... all with identical fields, just named differently such as R.xml.acct1, R.xml.acct2, etc... Then calling the appropriate R.xml file during the addPreferencesFromResources as well as the saving calls.
The question is, is that the correct or best way to do this? Is there something else I am missing? Or is using an SQL database the better way to handle this? Maybe I should just give up on preferences and use SQL and create my own input/edit screens?
Peter,
You can use the db if you are familiar with sqlite and all the code needed to handle it. That is not a bad option; however, it might be overkill for what you are doing.
If you want to use SharedPreferences, I would recommend creating xml files for each account and storing the data in an xml file. This will require you to create a view to capture the data and not use a preference fragment.
Here is some code that could help you get started for saving the data.
public static boolean setAccountInformation(final Context context, final String accountName, final Account account) {
SharedPreferences.Editor editor = context.getSharedPreferences(accountName, Context.MODE_PRIVATE).edit();
editor.putString("account_name", account.name);
editor.putString("account_email", account.email);
...
return editor.commit();
}
public static Account getAccountInformation(final Context context, final String accountName) {
Account account = new Account();
SharedPreferences preference = context.getSharedPreferences(accountName, Context.MODE_PRIVATE);
account.email = preference.getString("account_name", null);
account.name = preference.getString("account_email", null);
...
return account;
}
This will require you to keep track of the account (xml) file names. You could store them in a string array in another shared preference and add or remove the names when accounts are added or removed.
I'm trying to create a way to get input from a user and save the string in the string.xml, so that when I launch my Application again, it will be there. Can I use the string.xml or do I have to save it another way?
Can I use the string.xml or do I have to save it another way?
You will have to do something different. The resources can't be changed after it has been compiled.
Have a look at Storage Options to see which way is best for you. The best way will be determined by things such as the type and amount of data you will be storing.
The link I posted sums it up pretty well then you can dig into the structures that you think might work best for your situation. From the link:
Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.
in order to save your strings you can use SharedPreferences
to save your data
SharedPreferences pos;
public String fileName = "file";
pos = getSharedPreferences(fileName, 0);
SharedPreferences.Editor editor = pos.edit();
editor.putString("pwd","your string");
editor.commit();
to get your data
pos = getSharedPreferences(fileName, 0);
String data = pos.getString("pwd", "");
How do I save an integer that can be overwritten and then read it on android? I am using libgdx. I know how to do it for my desktop, but I want internal storage.
You should use libgdx Preferences.
That way it will work cross-plattform! (including Android)
Since you just want to store some Integer that should be just what you need.
See example:
//get a preferences instance
Preferences prefs = Gdx.app.getPreferences("My Preferences");
//put some Integer
prefs.putInteger("score", 99);
//persist preferences
prefs.flush();
//get Integer from preferences, 0 is the default value.
prefs.getInteger("score", 0);
There are different ways to do that:
You can write to a FileHandle. Just create a FileHandle like Gdx.files.local("myfile.txt"); and write to it using fileHandle.writeString(Integer.toString(myInt));
Note, that Internal and Classpath are read-only, so you can't write files there.
Also note, that not all types of the gdx.files. are usable for all backends. More on that here.
The second way to do that are the Preferences. The Preferences are the only way to have persistent data for HTML5 applications.
The Preferences are XML files in which you can store, read and change data.
To create Preferences for your app you just need to call:
Preferences myPref = Gdx.app.getPreferences("PreferenceName");
Note, that you should use the full name, for example com.stackoverflow.preferences, as on desktop all Preferences use the same file.
To write data you can use myPref.putInteger("name", value) for Integers, myPref.putBoolean(("name", value) for Booleans... Make sure you call myPref.flush() after all changes, to save the new data.
To get the data you can call myPref.getInteger("name, defaultValue"). The default value is returned if there is no data with the given name.
More on Preferences here.
Hope i could help.
While developing an android app, which is the best and efficient way to retrieve values from the below options:
To get values from a property file or
To get values from app shared preferences.
I like to know this based on memory usage and speed of retrieval.
Edit:
IMO there's no better way as those two options would be used for different purpose.
SharedPreference gives you an easy way to build a user editable Preference page via the PreferenceActivity and to share preference trough your application.
For other purpose as user editable settings you could use Properties files to store data.
For properties you have two good examples here:
http://www.mkyong.com/java/java-properties-file-examples/
http://myossdevblog.blogspot.com/2010/02/reading-properties-files-on-android.html
For SharedPreference see here:
http://developer.android.com/guide/topics/data/data-storage.html#pref
An example:
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
final String yourPref = sharedPreferences.getString(getString(R.string.settings_tag_your_pref), "");
or if you don't want to define a tag in string:
final String yourPref = sharedPreferences.getString("your_pref", "");
I'm currently trying to think of the best way to program an app which simply has text and images that update every day to different content. Would the best way to do this be to store all of the items in an array and then call upon each according to the phone's clock? Or is there a better or simpler way of doing this?
If you need to know I'm using Eclipse to program the app.
You could have the app check for updates each day or you could make a mobile website and use a webview to display the site ,and just update the site daily
Since you are using text and images, I would recommend storing them in your app's SharedPreferences. These keep data stored that your app can easily access to view or change. You can store text and bitmaps easily. For text:
SharedPreferences prefs = getSharedPreferences("MySharedPreferences", Activity.MODE_PRIVATE);
SharedPreferences.Editor preferences = prefs.edit();
String firstString = prefs.getString("MyFirstString");
//check if firstString has changed on the server
//for example, set a new String that you retrieve from the server to firstStringMaybe
if (firstString != firstStringMaybe) { //meaning you need to update firstString
prefs.putString("MyFirstString", firstStringMaybe);
prefs.commit();
}
Bitmaps storage is much more complicated, because you need to first Serialize the bitmap, then store it as a String. This probably means creating a new class that contains the bitmap object and implements Serializable. There are many examples available for how to do this:
http://www.johnwordsworth.com/2011/06/serializing-and-de-serializing-android-graphics-bitmap/
android how to save a bitmap - buggy code