Best way to retrieve values in android mobile apps - android

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", "");

Related

Why does SharedPreferences need two keys?

I'm trying to build a single object that handles all my SharedPreferences, since they are mostly used in the whole app, and I don't quite Understand why it takes two keys to get a value.
The call looks like this:
context.getSharedPreferences(FirstKey, Context.MODE_PRIVATE).getString(SecondKey, default)
I get that its basically built up as a two dimensional array.
The FirstKey gives me a collection of key-value pairs I can use my SecondKey on to get my value. And I get that If I have dozens of SharedPreferences this might come in handy to manage them and prevent mixups/unwanted overwriting.
But is this necessary If I only have like 10 preferences I save anyway or is it reasonable to just use one FirstKey for all of my preferences?
But is this necessary If I only have like 10 preferences I save anyway
or is it reasonable to just use one FirstKey for all of my
preferences?
So for this case you can avoid the use of that FirstKey by using getDefaultSharedPreferences() like this:
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
editor.putString("myKey", "myValue");
editor.apply();
or read already set preferences:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String myPref = prefs.getString("myKey", "myDefaultValue");
I guess it is to encapsulate, organize and manage better groups (first key values) of data you want to store (second keys values). So in case you want for example to retrieve all settings preferences you can group them by a Settings file (first key). Or in case you would like to delete all stored values regarding a user preferences (preferred language, preferred currency.. ) then you can organize those data within a "UserPref" file (first key) and then you can iterate within it to either delete all of them when you logout or whatever pogic you see useful for your user experience.
First key
Retrieve and hold the contents of the preferences file
Second key
Retrieve and hold value in this file
You can use only your "FirstKey" for all your preferences and normally dev us it like this only. You can create multiple instances of shared preference by changing the "First key" at the time of getSharedPreference(). Suppose you want two different shared preference for two different modules in your project then change the "FirstKey" parameter, in this case, you have two be careful while storing and fetching data from preference as you have two different shared preference.
As you rightfully stated, the first key represents a group of key-value pairs (which is actually very similar to a single file) and the second key helps you fetch the values in that file/group.
I personally believe this design is great especially in cases where you may want to separate all your values into different categories. If your app is "small", you can save all your values in one single file/group. Otherwise, you can split all your values into separate files/groups.
I hope this helps.. Merry coding!

SharedPreferences - Difference between these two ways of retrieving the Preference value?

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.

How do I save and read an integer on android libgdx?

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.

using shared preferences in android to fill data in forms

I am developing an android Application for managing and creating personal and business cards.
How can I use Shared Preferences (where i will be storing my personal and professional information ) in creating the cards.
Every time i need to create a new card, the program should ask me to gather information from the Shared Preferences and also my "create new Card" form should ask for adding new fields (like another or new e-mail not present in shared preferences ) dynamically.
please let me know any sample examples explaining such scenario..... I have extensively searched over internet to find some, but couldn't get any as per my requirement.
Thankyou.
For your requirement it should be better to use sqlite database. because if you are adding more records then it always better to use database for storing similar type of data..
otherwise if you want to use Shared Preference only then try like this...
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
//Storing data
Editor edit = preferences.edit();
edit.putString("pref_empId", _empid);
edit.putString("pref_userName", _username);
edit.putString("pref_userType", _usertype);
edit.commit();
//Retrieving
pref_userName = preferences.getString("pref_userName", "n/a");
pref_empId = preferences.getString("pref_empId","n/a");
pref_userType = preferences.getString("pref_userType","n/a");
In shared preferences you can store the info as key-value pair. For details read http://developer.android.com/guide/topics/data/data-storage.html#pref

Android daily update text app

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

Categories

Resources