Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence.

Slides:



Advertisements
Similar presentations
Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
Advertisements

User Preferences – List Views Select “User Preference” from the File Menu.
 data/data-storage.html#pref data/data-storage.html#pref 
Intermediate Level Course. Text Format The text styles, bold, italics, underlining, superscript and subscript, can be easily added to selected text. Text.
Creating a Users Table The users table contains a list of your site’s authorized users, logins, and passwords.
Preferences. 2 Persistent storage Information is persistent if it is kept between one invocation of a program and the next Many programs keep user preferences.
ANDROID PROGRAMMING MODULE 1 – GETTING STARTED
Android 4: Creating Contents Kirk Scott 1. Outline 4.1 Planning Contents 4.2 GIMP and Free Sound Recorder 4.3 Using FlashCardMaker to Create an XML File.
Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need.
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.
Prepared by: Jennifer McKee With support from: in partnership with: Introduction to ArcPad NSF DUE
CRYPTOGRAPHY PROGRAMMING ON ANDROID Jinsheng Xu Associate Professor North Carolina A&T State University.
Data Storage: Part 1 (Preferences)
Mobile Programming Lecture 1 Getting Started. Today's Agenda About the Eclipse IDE Hello, World! Project Android Project Structure Intro to Activities,
Scottish Legal Aid Board Content Management using OpenCms Martin Spinks CTO Navyblue Tuesday, March 16, 2010.
CS5103 Software Engineering Lecture 08 Android Development II.
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
The NetBeans IDE CSIS 3701: Advanced Object Oriented Programming.
Introduction to Computers Seminar I. Parts of the Computer Personal Computer a PC (any non-Mac computer) has four major pieces of hardware-- keyboard,
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.
Otasuke GP-EX! Chapter 11 GP-Viewer EX
SQLite Supported by BlackBerry OS 5.0 Using SQLite.
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.
Persistent Storage using Perst Lite in Android. Introduction  Android by default uses SQLLite to handle data storage  SQLLite is a relational database.
Data Persistence Nasrullah Niazi. Share Preferences The SharedPreferences class provides a general framework that allows you to save and retrieve persistent.
Diagnostic Pathfinder for Instructors. Diagnostic Pathfinder Local File vs. Database Normal operations Expert operations Admin operations.
® Microsoft Office 2013 Access Creating a Database.
Computer Applications I Competency 3 – Use handheld devices. Objective 3.03 – Use handheld devices and handheld applications software to input and edit.
ITGS Databases.
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.
Introduction to Android
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.
How to Setup and Score a Tournament May Let’s Get Organized The setup and organization outlined in this clinic are suggested steps however can be.
SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.
Android: “Dynamic” data and Preferences data.
Getting to Know Your Desktop Icons, Taskbar, Workspace, Window, Notifications, Start and Search.
© 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.
Preferences. 2 Persistent storage Information is persistent if it is kept between one invocation of a program and the next Many programs keep user preferences.
Project Description MintTrack is a mobile application built for the Android OS that will help keep track of where a user’s money is being spent via expense.
CHAPTER 9 File Storage Shared Preferences SQLite.
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.
Editing a Twitter search. Viewing search results in a browser.
Why Learn Android? Largest installation base of any operating system Over 20,000 Android devices exist Businesses will likely move more to device-based.
Data Persistence Chapter 9. Objectives Learn about data storage methods Understand use of Shared Preferences Understand file-based storage and the differences.
CS371m - Mobile Computing Persistence.
Android Application Data Storage 1.
Chapter 2 Starting a Project
Android Studio, Android System Basics and Git
Mobile Application Development Chapter 5 [Persistent Data in Android]
Part 3 – Remote Connection, File Transfer, Remote Environments
Android Activities An application can have one or more activities, where Each activity Represents a screen that an app present to its user Extends the.
Mobile Computing With Android ACST 4550 Android Data Storage
Android Storage.
More than just File Sync and Share.
Web Page Design CIS 300.
3.1 Basic Concept of Directory and Sub-directory
Preferences.
Preferences.
Preferences.
Preferences.
Preferences.
Preferences.
Implication of Orientation Changes
Preference Activity class
Presentation transcript:

Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence

SharedPreferences Saving simple application data like – Text strings – Preferred font size, background color, etc. – Example: PreferenceMenuExample 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(); OR BETTER editor.apply(); 2Data 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 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 persistence3

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 getAll() SharedPreferences.Editor edit() Data persistence4

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 persistence5

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 You can find the preferences file – From Android Studio open Device Monitor → File Explorer → data → data → appName → shared_prefs – To see the contents of the file: Pull a file from device (small red/black icon top-left) Data persistence6

Files The basic file API classes from the ordinary Java package java.io – FileInputStream, InputStreamReader, BufferedReader, etc. However, opening a file is different – FileInputStream fis = openFileInput(someName, MODE_PRIVATE); – 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: FilesExample + FilesExampleAsyncTask Data persistence7

Databases: SQLite Android comes with a small scale relational DBMS called SQLite – Uses ordinary SQL The database file is stored in the folder – /data/data/ /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: DatabaseExample Data persistence8

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 persistence9

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 persistence10

Foreign keys SQLite claims to have support for foreign keys starting from version – – Foreign key checking must explicitly be switched on – PRAGMA foreign_keys = on Data persistence11

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 persistence12