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

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
 data/data-storage.html#pref data/data-storage.html#pref 
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Tutorial 11: Connecting to External Data
Microsoft Office Word 2013 Expert Microsoft Office Word 2013 Expert Courseware # 3251 Lesson 4: Working with Forms.
Data Persistence in Android
Data Access Patterns. Motivation Most software systems require persistent data (i.e. data that persists between program executions). In general, distributing.
Data Storage: Part 1 (Preferences)
SQLite Database. SQLite Public domain database – Advantages Small (about 150 KB) – Used on devices with limited resources Each database contained within.
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
Cosc 5/4730 Android Content Providers and Intents.
Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.
COMP 365 Android Development.  Manages access from a central database  Allows multiple applications to access the same data.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal.
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems Recitation.
Data Structure & File Systems Hun Myoung Park, Ph.D., Public Management and Policy Analysis Program Graduate School of International Relations International.
Android Storage. There are several options for storage of data with Android We can put data into a preferences file. We can put data into a ‘normal’ file.
Nilesh Singh Local Data Storage option Android provides several options for you to save persistent application data. - Shared preferences - Creation.
Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence.
9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
Dr. Azeddine Chikh IS444: Modern tools for applications development.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Persistence Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Mobile Software Development ISCG 7424 Department of Computing UNITEC John Casey and Richard Rabeder SQLite and Permissions.
SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.
SQlite. SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Building User Interfaces Basic Applications
CHAPTER 9 File Storage Shared Preferences SQLite.
Android Storage MAY 2013 Hu.Cai. NAME OF PRESENTATION [CHANGE IN SLIDE MASTER] MONTH, YEAR [CHANGE IN SLIDE MASTER] Outline 1.Storage In General 2.SharedPreferences.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
Mobile Application Development Data Storage. Android provides several options for you to save persistent application data. The solution you choose depends.
Data Storage in Android Димитър Н. Димитров. Why talk about data? Why not 3D graphics or network connectivity? Data as fundamental term in computer science.
Content Providers.
3 A Guide to MySQL.
Android Content Providers & SQLite
Mobile Applications (Android Programming)
Data Storage: Part 2 (File System)
CS371m - Mobile Computing Persistence.
Android Application Data Storage 1.
Creating Oracle Business Intelligence Interactive Dashboards
GO! with Microsoft Office 2016
Mobile Software Development for Android - I397
Using E-Business Suite Attachments
Android Application SQLite 1.
CaRT eCapacity Initiative Ghana Productivity Apps
Android Database using SQLite
Mobile Application Development Chapter 5 [Persistent Data in Android]
Activities and Intents
GO! with Microsoft Access 2016
Microsoft Office Illustrated
Mobile Computing With Android ACST 4550 Android Data Storage
Android Storage.
Topics Introduction to File Input and Output
NORMA Lab. 5 Duplicating Object Type and Predicate Shapes
HTML5 and Local Storage.
Mobile Computing With Android ACST 4550 Android Database Storage
Database Applications
Topics Introduction to File Input and Output
Mobile Programming Dr. Mohsin Ali Memon.
Unit J: Creating a Database
Preference Activity class
Executive Reports, Instructions and Documentation
Presentation transcript:

Data Persistence Chapter 9

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Database with SQLite SQLite database system ( 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

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

Example Table Schema 20

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

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

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

Lab 9-3 Pet Contacts Pages 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