Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 7

Saving Key-Value Sets

• SharedPreferences API
– If you have a relatively small collection of key-values
that you'd like to save, you should use it.
– SharedPreferences object points to a file containing
key-value pairs and provides simple methods to read
and write them.
– Each SharedPreferences file is managed by the
framework and can be private or shared.
Get a Handle to a SharedPreferences
• You can create a new shared preference file or
access an existing one by calling one of two
methods:
– getSharedPreferences()
• Use this if you need multiple shared preference files
identified by name, which you specify with the first
parameter. You can call this from any Context in your app.
– Example
SharedPreferences sharedPref =
context.getSharedPreferences(”FileName”, Mode);
Get a Handle to a SharedPreferences
• You can create a new shared preference file or
access an existing one by calling one of two
methods:
– getPreferences()
• Use this from an Activity if you need to use only one shared
preference file for the activity. Because this retrieves a
default shared preference file that belongs to the activity,
you don't need to supply a name.
– Example
SharedPreferences sharedPref = getPreferences(Mode);
Saving Key-Value Sets
• Mode may be one of the following:
– Context.MODE_PRIVATE
• File creation mode: the default mode, where the created file
can only be accessed by the calling application (or all
applications sharing the same user ID).
Saving Key-Value Sets
– Context.MODE_WORLD_READABLE
• This constant was deprecated in API level 17. Creating world-
readable files is very dangerous, and likely to cause security
holes in applications. It is strongly discouraged; instead,
applications should use more formal mechanism for
interactions such as ContentProvider, BroadcastReceiver,
and Service. There are no guarantees that this access mode
will remain on a file, such as when it goes through a backup
and restore. File creation mode: allow all other applications
to have read access to the created file.
Saving Key-Value Sets
– Context.MODE_WORLD_WRITEABLE
• This constant was deprecated in API level 17. Creating world-
writable files is very dangerous, and likely to cause security
holes in applications. It is strongly discouraged; instead,
applications should use more formal mechanism for
interactions such as ContentProvider, BroadcastReceiver,
and Service. There are no guarantees that this access mode
will remain on a file, such as when it goes through a backup
and restore. File creation mode: allow all other applications
to have write access to the created file.
Reading and Writing Values
• Supported types
• Boolean, Float, Integer, Long, String
• Writing a value
– Example
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("variable-name", "value");
editor.commit();

• Reading a value
– Example
– String
sValue=sharedPref.getString("variable-name", "default-value");

You might also like