130 likes | 512 Views
Data persistence. How to save data using: SharedPreferences, Files, a nd SQLite database. SharedPreferences. Saving simple application data like Text strings Preferred font size, background color, etc. Reading preferences, example
E N D
Data persistence How to save data using: SharedPreferences, Files, and SQLite database Data persistence
SharedPreferences • Saving simple application data like • Text strings • Preferred font size, background color, etc. • Reading preferences, example SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE); float fontSize = prefs.getFloat(FONT_SIZE_KEY, 12); // default value: 12 editText.setText(prefs.getString(TEXT_VALUE_KEY, "")); // default value: empty string • Writing preferences, example SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE); // MODE_PRIVATE: Only accessible to this application SharedPreferences.Editor editor = prefs.edit(); editor.putFloat(FONT_SIZE_KEY, editText.getTextSize()); editor.putString(TEXT_VALUE_KEY, editText.getText().toString()); editor.commit(); Data persistence
SharedPreferences:Obtaining the object • The way to get access to a SharedPreferences object it through the method • SharedPreferences getSharedPreferences(String name, int mode) • String name: The name of the preferences object • Int mode: Decides which application can access the object • Factory method design pattern • Comparable to Logger.getLogger(String name) from the ordinary Java API • Modes • MODE_PRIVATE • Only accessible to the creating application • Any Activity in the creating application can read and write to the object • MODE_WORLD_READABLE • Any application can read the object. • Only the creating application can write to the object • MODE_WORLD_WRITEABLE • Any application can read and write to the object Data persistence
SharedPreferences: Some methods • Int getInt(String key, int defaultValue) • General • Type getType(String key, Type defaultValue) • Type: float, long, int, String, etc. • boolean contains(String key) • Map<String, ?> getAll() • SharedPreferences.Editor edit() Data persistence
SharedPreferences.Editor: some methods • SharedPreferences.Editor putType(Type element) • Type: float, long, int, String, etc. • SharedPreferences.Editor remove(String key) • Removes one entry • SharedPreferences.Editor clear() • Removes all entries • Void apply() + boolean commit() Data persistence
Using SharedPreferences • In most cases a SharedPreferences object should only be accessible in a single application • MODE_PRIVATE • Can be used to hold information about the users preferences or personal data • Background color, birth day, name etc. • Username + password for easy login. Security?? • Can be used to transport data between Activities • An alternative to putting the data into an Intent object. • However, data is going to last longer with SharedPreferences • Example: UsingPreferences • You can find the preferences file • Eclipse DDMS prespective → File Explorer → data → data → appName→ shared_prefs Data persistence
Files • The basic file API classes from the ordinary Java package java.io • FileInputStream, InputStreamReader, BufferedReader, etc. • However, opening a file is different • FileInputStreamfis = openFileInput(someName, MODE_WORLD_READABLE); • openFileInput is yet another method from the Activity class • You can create files (text or binary) and read them • Files can be stored • On the device’s internal store • On an SD memory card • For transportation to another device • Usage • High score lists, pictures, etc. • Example: Lee209Files Data persistence
Databases: SQLite • Android comes with a small scale relational DBMS called SQLite • http://sqlite.org/ • Uses ordinary SQL • The database file is stored in the folder • /data/data/<package name>/databases • Can be seen in the debugger tab File Explorer • File Explorer can be opening from the menu Window -> Show View • A database is only accessible to the application which created the database • Secure • The database must be created by the application using it • Usually you will create the database the first time you use the application • No DBMS tools/user interfaces for your phone! • Example: Lee218SQLite Data persistence
Database programming practice • It’s good practice with SQLite (and any other DBMS) to put the database related code in a separate class • This calls is usually called DBAdapter • This class is NOT an Activity, but a plain Java class Data persistence
SQLite database versions • When you create a SQLite database you should must give it a version number • Start using version 1. • You cannot alter the tables, etc. In the database • What you can do is that you can create a new database with a new version number • And all your old data is lost Data persistence
Foreign keys? • SQLite claims to have support for foreign keys starting from version 3.6.19 • http://www.sqlite.org/foreignkeys.html • http://www.sqlite.org/faq.html#q22 • Users have reported that the foreign key syntax is allowed, but that foreign keys are not checked at INSERT, UPDATE, and DELETE Data persistence
SQLite, a few more words • SQLite can run on ordinary computers as well as on mobile devices. • SQLite comes with a lot of options • Some of which can make SQLite run faster Data persistence