Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Persistence Chapter 9. Objectives Learn about data storage methods Understand use of Shared Preferences Understand file-based storage and the differences.

Similar presentations


Presentation on theme: "Data Persistence Chapter 9. Objectives Learn about data storage methods Understand use of Shared Preferences Understand file-based storage and the differences."— Presentation transcript:

1 Data Persistence Chapter 9

2 Objectives Learn about data storage methods Understand use of Shared Preferences Understand file-based storage and the differences between internal and external storages Learn how to build apps using SQLite databases 2

3 Storing Data Applications often need to store various application-specific information such as … 3

4 Storing Data Applications often need to store various information including: User’s preference to provide a more sophisticated level of personalization and responsiveness Login credentials Settings to persist across sessions Applications are frequently data-driven and require the management of a larger volume of data. Databases and files can be used by an application to store structured data or information that will be made available to other applications (Content providers). 4

5 Storing Data (Cont.) How to choosing the manner in which data is stored? Depends upon the specific needs of the application, e.g., Amount of data that needs to be stored Whether the data will be kept private Two different storages Device’s internal memory and external storage media Files that are saved within the device’s internal storage memory tend to be small, as opposed to external storage, which typically holds larger volume files. 5

6 Storing Data (Cont.) Three ways for persisting data Shared preferences: lightweight mechanism to save small chunk of data in key-value pairs Traditional file systems (internal or external storage) RDBMS (SQLite---serverless SQL database engine) 6

7 Shared Preferences Storage of a limited set of primitive data including strings Used to store a user’s personal settings and a small amount of application session data Simple way to read and write key-value pairs of data Proprietary and inaccessible to outside applications Stored in XML files in internal storage Allows preference data values to automatically persist throughout sessions, regardless of how they end, e.g., exit of an application, shutdown of the device, or system crash 7

8 Shared Preferences (Cont.) Stored in XML files in internal storage /data/data/ /shared_prefs/ Hi! 8

9 SharedPrefereces API Creating SharedPreferences Context.getSharedPreference(String, int) SharedPreferences Context.getPreference(int) Default name: Activity class name, e.g., edu.utep.cs.cs4390.Omok_preferences.xml Mode: MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITABLE, etc. Q: Private? To activity, not application 9

10 SharedPrefereces API Editing SharedPreferences.Editor SharedPreference.edit() void SharedPreferences.Editor.putFloat(String, float) void SharedPreferences.Editor.putString(String, String) … void SharedPreferences.Editor.commit() Querying Float SharedPreferences.getFloat(String, float) … PreferenceActivity Special activity to create, display, and edit a hierarchy of preferences Perferences can be defined in XML and associated with it, e.g., addPreferencesFromResource(R.xml.my_app_preferences). 10

11 In-Class (Pair) Write a login app that prompt for a user name and a password and optionally save the entered user name and password. Use a check box labeled “Remember?” Use a toast message to indicate whether the credential is being saved or not. 11 User name: android Password: ****** [V] Remember? Login

12 File Storage Allow data to be written to an actual file structure Require more control regarding read and write permissions Internal vs. external storage Internal: device’s built-in memory External: Secure Digital (SD) card Significant differences in how internal and external storage is utilized in an application 12

13 Internal Storage Allow data to be written to be stored directly onto the device’s memory Always available, assuming there is space Can be configured to be readable and writeable by an application By default, files saved to internal storage are private to the application. Typically, internal storage is utilized when processing an image, video and audio elements, and large data files. 13

14 API Use java.io package Creating or opening files FileOutputStream Context.openFileOutput(String, int) FileInputStream Context.openFileInput(String) FileOutputStream out = openFileOutput(“myfile.txt”, MODE_PRIVATE); // mode: MODE_PRIVATE, MODE_APPEND, MODE_WORLD_WRITABLE // Creates /data/data/ /files/myfile.txt FileInputStream in = openFileInput(“myfile.txt") 14

15 External Storage May not always be obtainable on a device Is publicly shared storage, which means that it can be made available to external applications. Unlike internal storage, even if an application is uninstalled, external storage files will continue to exist. 15

16 API Use java.io package Need to add WRITE_EXTERNAL_STORAGE permission to AndroidManifest.xml Q: Root directory of external storage? File sdCard = Environment.getExternalStorageDirectory(); return the full path to the external directory, e.g., /sdcard android.os.Environment: provide access to environment variables, e.g., File getDataDirectory() File getRootDirectory() File getDownloadCacheDirectory()... 16

17 Static Resources Files added to the package during design (e.g., help files) Reside in the res/raw folder (need to be created) API InputStream is = getResources().openRawResource(R.raw.textfile); 17

18 Database with SQLite SQLite database system (www.sqlite.org) Serverless database engine supporting SQL Managing private and embedded databases in an application Stored in /data/data/ /database folder Can create and use database programmatically Can be created using open source tools (e.g., SQLite Database Browser) and bundled with an app (in assets), e.g., copy DB files 18

19 SQLite Database Collection of data organized in a table Database table structured by a set of rows and columns Row: a single record Column: a data field, an attribute of a data record Field: a single item that exists at the intersection between one row and one column Limited data types NULL, INTEGER, REAL, TEXT, and BLOB BLOB (Binary Large Object): store a large array of binary data (bytes) 19

20 Example Table Schema 20

21 SQLiteOpenHelper A framework class for working with SQLite databases, similar to DB adapter. Essential for the creation and management of database content, as well as database versioning Must be subclassed Must contain the implementation of onCreate() and onUpgrade() Can define methods such as adding, retrieving, deleting, updating records. 21

22 AdapterViews and Adapters Common to use adapter views to display data retrieved from database AdapterView Container widget that is populated by a data source, determined by an Adapter ListViews, GridViews, and Spinners Adapter: mechanism that binds a data source, such as a database, to an AdapterView 22

23 Lab 9-2 ToDo Today Pages 769-784 Simple text data Features ListView Custom adapter 23

24 Lab 9-3 Pet Contacts Pages 784-806 Image data Features TabHost – tabbed view Context menu FIX on p. 799 (MainActivity) com.cornez.petcontacts/drawable/none.png” ===> edu.utep.cs.cs4330.petcontacts/” + R.drawable.none Format: “android.resource:// / ” 24


Download ppt "Data Persistence Chapter 9. Objectives Learn about data storage methods Understand use of Shared Preferences Understand file-based storage and the differences."

Similar presentations


Ads by Google