Android Training (Storing & Shared Preferences)
-
Upload
khaled-anaqwa -
Category
Technology
-
view
163 -
download
0
Transcript of Android Training (Storing & Shared Preferences)
Android Data
StorageAndroid Training
By Khaled Anaqwa
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.
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).
To work with shared
preferences you need to
Initialization
Storing Data
Commit changes
Retrieving Data
Clearing / Deleting Data
InitializationApplication 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();
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.
Storing DataYou 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
Commit changesCall editor.commit() in order to save changes to shared
preferences..
like
editor.commit();
Retrieving DataData 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
Clearing / Deleting DataIf 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
Task Create Settings with ShaerdPresferance
General Settings
Language selection
Text Size
Textcolor
System Settings
Enable Notifications
use Wifi
About
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.
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().
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos =
openFileOutput(FILENAME,Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
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().
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).
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.
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.