Android Training (Storing & Shared Preferences)

19
Android Data Storage Android Training By Khaled Anaqwa

Transcript of Android Training (Storing & Shared Preferences)

Page 1: Android Training (Storing & Shared Preferences)

Android Data

StorageAndroid Training

By Khaled Anaqwa

Page 2: Android Training (Storing & Shared Preferences)

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.

Page 3: Android Training (Storing & Shared Preferences)

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).

Page 4: Android Training (Storing & Shared Preferences)

To work with shared

preferences you need to

Initialization

Storing Data

Commit changes

Retrieving Data

Clearing / Deleting Data

Page 5: Android Training (Storing & Shared Preferences)

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();

Page 6: Android Training (Storing & Shared Preferences)

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.

Page 7: Android Training (Storing & Shared Preferences)

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

Page 8: Android Training (Storing & Shared Preferences)

Commit changesCall editor.commit() in order to save changes to shared

preferences..

like

editor.commit();

Page 9: Android Training (Storing & Shared Preferences)

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

Page 10: Android Training (Storing & Shared Preferences)

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

Page 11: Android Training (Storing & Shared Preferences)

Task Create Settings with ShaerdPresferance

General Settings

Language selection

Text Size

Email

Textcolor

System Settings

Enable Notifications

use Wifi

About

Page 12: Android Training (Storing & Shared Preferences)

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.

Page 13: Android Training (Storing & Shared Preferences)

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().

Page 14: Android Training (Storing & Shared Preferences)

String FILENAME = "hello_file";

String string = "hello world!";

FileOutputStream fos =

openFileOutput(FILENAME,Context.MODE_PRIVATE);

fos.write(string.getBytes());

fos.close();

Page 15: Android Training (Storing & Shared Preferences)

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().

Page 16: Android Training (Storing & Shared Preferences)

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).

Page 17: Android Training (Storing & Shared Preferences)

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.

Page 18: Android Training (Storing & Shared Preferences)

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.

Page 19: Android Training (Storing & Shared Preferences)