We saw in an earlier tutorial (Part 12) how data can be
stored in SQLDB. However, many applications may provide a way to capture user
preferences on the settings of a specific application or an activity. For
supporting this, Android provides a simple set of APIs.
Preferences are typically name value pairs. They can
be stored as “Shared Preferences” across various activities in an application
(note currently it cannot be shared across processes). Or it can be something
that needs to be stored specific to an activity (which is not discussed here).
The context object lets you retrieve SharedPreferences
through the method Context.getSharedPreferences().
In my example, I will set 2 preferences i.e. MyName
and MyWallpaper
in one activity i.e ManageSharedPref.java. Retrieve
these values in the next activity – ViewSharedPrefs.java.
The second activity displays my preferred name in a list view and also resets
the android wallpaper to the image that I had set as a preferred wallpaper in
the first activity. When you run this application, if you come back to the
home, you will see the wall paper is reset.
Here is the code in ManageSharesPrefs:
SharedPreferences
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor =
myPrefs.edit();
prefsEditor.putString(MY_NAME, "Sai");
prefsEditor.putString(MY_WALLPAPER, "f664.PNG");
prefsEditor.commit();
First, I obtain a SharedPreferences object
making it readable by all. The first parameter is a name of a file that stores
my preferences. This automatically creates the xml file if it does not exist
and then stores in the same.
Next, I edit it. That creates an editor object, using
which I input my preferences. Here, for the wall paper, I have put an image
name. I also need to push the actual image file into the android storage which
I do this way.
adb push <local> <remote>
In this case it is
adb push f664.PNG
/data/misc/wallpaper/f664.PNG
This command creates a folder called wallpaper in
/data/misc and copies the f664.PNG file from my current location to the android
storage.
When I click the “View Shared Preferences” button, I am
taken to the next activity. Here is the code in ViewSharedPrefs that gets
executed:
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs.getString(MY_NAME, "nothing");
String wallPaper = myPrefs.getString(MY_WALLPAPER, null);
if(wallPaper != null) {
try {
Bitmap
bm = BitmapFactory.decodeFile("/data/misc/wallpaper/"+wallPaper);
Log.d(getClass().getSimpleName(),"Wallpaper name is: "+ wallPaper);
setWallpaper(bm);
Toast.makeText(this, "Wall paper
has been changed." +
"You may go to the home screen to view the same", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException fe){
Log.e(getClass().getSimpleName(),"File not found");
}
catch (IOException ie) {
Log.e(getClass().getSimpleName()," IO Exception");
}
}
ArrayList<String> results = new ArrayList<String>();
results.add("Your Preferred name is: " + prefName);
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
There are 3 steps to understand here:
1. Step 1: Retrieve
the shared prefs data from the object.
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs.getString(MY_NAME, "nothing");
String wallPaper = myPrefs.getString(MY_WALLPAPER, null);
2. Step 2: Display
the name
ArrayList<String> results = new ArrayList<String>();
results.add("Your Preferred name is: " + prefName);
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
3. Step 3: Reset the
wall paper.
if(wallPaper != null) {
try {
Bitmap
bm = BitmapFactory.decodeFile("/data/misc/wallpaper/"+wallPaper);
Log.d(getClass().getSimpleName(),"Wallpaper name is: "+ wallPaper);
setWallpaper(bm);
Toast.makeText(this, "Wall paper
has been changed." +
"You may go to the home screen to view the same", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException fe){
Log.e(getClass().getSimpleName(),"File not found");
}
catch (IOException ie) {
Log.e(getClass().getSimpleName()," IO Exception");
}
}
It is this simple. The complete code is available here.
