2. Your data storage options are
the following:
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.
3. SharedPreferences
The SharedPreferences class provides a
general framework that allows you to
save and retrieve persistent key-value
pairs of primitive data types.
This data will persist across user sessions
(even if your application is killed).
4. To work with shared
preferences you need to
Initialization
Storing Data
Commit changes
Retrieving Data
Clearing / Deleting Data
5. Initialization
Application shared preferences can be fetched
using getSharedPreferences() method.
You also need an editor to edit and save the
changes in shared preferences.
SharedPreferences pref =
getApplicationContext().getSharedPreferences(”FileName", 0); // 0 - for
private mode
Editor editor = pref.edit();
6. Application shared preferences
can be fetched using
getSharedPreferences() - Use this if you
need multiple preferences files identified
by name, which you specify with the first
parameter.
getPreferences() - Use this if you need
only one preferences file for your Activity.
Because this will be the only preferences
file for your Activity, you don't supply a
name.
7. Storing Data
You can save data into shared preferences using editor. All the
primitive data types like booleans, floats, ints, longs, and strings
are supported.
You need to put data as Key with Value.
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long
9. Retrieving Data
Data can be retrived from saved preferences by calling
getString() (For string) method.
Remember this method should be called on Shared Preferences
not on Editor.
// returns stored preference value
// If value is not present return default value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean
10. Clearing / Deleting Data
If you want to delete from shared preferences you can call
remove(“key_name”) to delete that particular value.
If you want to delete all the data, call clear().
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes;
editor.clear(); //clear all the data from shared preferences
editor.commit(); // commit changes
11. Task
Create Settings with ShaerdPresferance
General Settings
Language selection
Text Size
Email
Textcolor
System Settings
Enable Notifications
use Wifi
About
12. Using the Internal Storage
You can save files directly on the device's
internal storage. By default, files saved to
the internal storage are private to your
application and other applications
cannot access them.
When the user uninstalls your application,
these files are removed.
13. To create and write a private
file to the internal storage:
Call openFileOutput() with the name of
the file and the operating mode. This
returns a FileOutputStream.
Write to the file with write().
Close the stream with close().
15. To read a file from internal
storage:
Call openFileInput() and pass it the name
of the file to read. This returns a
FileInputStream.
Read bytes from the file with read().
Then close the stream with close().
16. RAW Files
If you want to save a static file in your
application at compile time, save the file
in your project res/raw/ directory.
You can open it with
openRawResource(), passing the
R.raw.<filename> resource ID.
This method returns an InputStream that
you can use to read the file (but you
cannot write to the original file).
17. Cache Files:
If you'd like to cache some data, rather
than store it persistently, you should use
getCacheDir() to open a File that
represents the internal directory where
your application should save temporary
cache files.
18. PreferenceActivity
This is the base class for an activity to
show a hierarchy of preferences to the
user.
this functionality should now be found in
the new PreferenceFragment class.
using PreferenceActivity in its old mode,
the documentation there applies to the
deprecated APIs here.