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.

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Android og persistens
Cosc 5/4730 Android: “Dynamic” data.. Saving Dynamic data. While there are a lot of ways to save data – Via the filesystem, database, etc. You can also.
 data/data-storage.html#pref data/data-storage.html#pref 
Tracking & Login Data persistence User tracking.
Working With Data on Titanium Local Data Remote Data Titanium Data Options:
Android Development: File System Approaches Richard S. Stansbury 2015.
CS371m - Mobile Computing Audio.
NovaBACKUP 10 xSP Technical Training By: Nathan Fouarge
File System. NET+OS 6 File System Architecture Design Goals File System Layer Design Storage Services Layer Design RAM Services Layer Design Flash Services.
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)
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
Education and New Technology Digital Cameras. What is a Digital Camera? Takes video, photographs, and sometimes sounds digitally by recording images through.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 3: Operating-System Structures System Components Operating System Services System Calls System Programs System Structure Virtual Machines System.
Data File Access API : Under the Hood Simon Horwith CTO Etrilogy Ltd.
Cosc 5/4730 Android Content Providers and Intents.
Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems Recitation.
PROG Mobile Java Application Development PROG Mobile Java Application Development Memory and Memory Issues Data Storage.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 3: Operating-System Structures System Components Operating System Services.
Data Structure & File Systems Hun Myoung Park, Ph.D., Public Management and Policy Analysis Program Graduate School of International Relations International.
Serialization. Serialization is the process of converting an object into an intermediate format that can be stored (e.g. in a file or transmitted across.
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.
Data Persistence Nasrullah Niazi. Share Preferences The SharedPreferences class provides a general framework that allows you to save and retrieve persistent.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
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.
Computer Hardware. What is Hardware? Hardware is any part of a computer you can touch There are 2 categories: Inside the computer Peripherals connected.
SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.
PROG Mobile Java Application Development PROG Mobile Java Application Development Data Storage, Continued.
Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using.
CS378 - Mobile Computing Audio.
Scanner  We can use it to convert printed images or docs into a digital format.  After obtaining a digital image we can make some modifications it on.
SOCSAMS e-learning Dept. of Computer Applications, MES College Marampally FILE SYSTEM.
© 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.
CHAPTER 9 File Storage Shared Preferences SQLite.
Exploring Networked Data and Data Stores Lesson 3.
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.
System Components Operating System Services System Calls.
Phonegap Bridge – Storage CIS 136 Building Mobile Apps 1.
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.
CS371m - Mobile Computing Audio. Audio on Device Devices have multiple audio streams: – music, alarms, notifications, incoming call ringer, in call volume,
Data Persistence Chapter 9. Objectives Learn about data storage methods Understand use of Shared Preferences Understand file-based storage and the differences.
Audio and Haptic Feedback
Data Storage: Part 2 (File System)
CS371m - Mobile Computing Persistence.
Android Application Data Storage 1.
REST APIs and Android Presented by: James Simshaw
Mobile Software Development for Android - I397
3 Introduction to Classes and Objects.
Play Framework: Introduction
Mobile Application Development Chapter 5 [Persistent Data in Android]
Mobile Computing With Android ACST 4550 Android Data Storage
Chapter 13: File Input and Output
Android Storage.
Reactive Android Development
Mobile Programming Dr. Mohsin Ali Memon.
Preference Activity class
Presentation transcript:

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 to reclaim resources but may be recreated later 2

Storing Data Multiple options for storing data associated with apps Shared Preferences Internal Storage – device memory External Storage SQLite Database Network Connection 3

Sharing Data Private data can be shared by creating a Content Provider Android has many built in Content Providers for things such as – audio (random song from last time) – images – video – contact information 4

Shared Preferences Private primitive data stored in key-value pairs SharedPreferences Class Store and retrieve key-value pairs of data – keys are Strings – values are Strings, Sets of Strings, boolean, float, int, or long Not strictly for preferences 5

Using SharedPreferences Obtain a SharedPreferences object for application using these methods: – getSharedPreferences(String name, int mode) if you want multiple files – getPreferences(int mode) 6

SharedPreferences Modes File creation modes Constants from the Context class – Activity is a descendant of Context MODE_PRIVATE – accessed only by calling application MODE_WORLD_READABLE – other applications have read access MODE_WORLD_WRITEABLE – other applications have write access MODE_MULTI_PROCESS – file on desk checked for modification even if shared preferences instance loaded. (Multiple threads using the same file) 7

Writing to SharedPreferences After obtaining SharedPreferences object: – call edit() method on object to get a SharedPreferences.Editor object – place data by calling put methods on the SharedPreferences.Editor object – also possible to clear all data or remove a particular key 8

Writing to SharedPreferences When done writing data via the editor call either apply() or commit() apply() is the simpler method – used when only one process expected to write to the preferences object commit() returns a boolean if write was successful – for when multiple process may be writing to preferences 9

Reading From Shared Preferences After obtaining SharedPreferences object use various get methods to retrieve data Provide key (string) and default value if key is not present get Boolean, Float, Int, Long, String, StringSet getAll() returns Map with all of the key/value pairs in the preferences 10

Shared Preferences File Stored as XML 11

Preference Activity An Activity framework to allow user to select and set preferences for your app tutorial 6 has an example – difficulty, sound, color, victory message Main Activity can start a preference activity to allow user to set preferences 12

Internal Storage Private data stored on device memory More like traditional file i/o by default files are private to your application – other apps cannot access files removed when app is uninstalled 13

Internal Storage To create and write a private file to the device internal storage: call openFileOutput(String name, int mode) – method from Context – file created if does not already exist – returns FileOutputStream object (regular Java class) Modes same as SharedPreferences minus MODE_MULTI_PROCESS and addition of MODE_APPEND 14

Writing to Files FileOutputStream writes raw bytes – arrays of bytes or single bytes Much easier to wrap the FileOutputStream in PrintStream object 15

Reading from Files files saved to device – data directory for app call openFileInput(String name) method to obtain a FileInputStream FileInputStream reads bytes – for convenience may connect to Scanner object or wrap in a DataInputStream object 16

Static Files If you need / have a file with a lot of data at compile time: – save file in project res/raw / directory – can open file using the openRawResource(int id) method and pass the R.raw.id of file – returns an InputStream to read from file – cannot write to the file 17

Cache Files If need to cache data for application instead of storing persistently: – call getCacheDir() method to obtain a File object that is a directory where you can create and save temporary cache files – files may be deleted by Android later if space needed but you should clean them up on your own – recommended to keep under 1 MB 18

External Files - Other Useful Methods All of these are inherited from Context File getFileDir() – get absolute path to filesystem directory when app files are saved File getDir(String name, int mode) – get and create if necessary a directory for files boolean deteleFile(String name) – get rid of files, especially cache files String[] fileList() – get an array of Strings with files associated with Context (application) 19

External Storage Public data stored on shared external storage may be SD (Secure Digital) card on non removable files saved to external storage are world- readable files may be modified by user when they enable USB mass storage for device 20

Checking Media Availability Call Environment.getExternalStorageState() method to determine if media available – may be mounted to computer, missing, read-only or in some other state that prevents accessing 21

Checking Media State other states such as media being shared, missing, and others 22

Accessing Files on External Storage call getExternalFilesDir(String type) to obtain a directory (File object) to get directory to save files type is String constant from Environment class – DIRECTORY_ALARMS, DIRECTORY_DCIM (Digital Camera IMages), DIRECTORY_DOWNLOADS, DIRECTORY_MOVIES, DIRECTORY_MUSIC, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES 23

External File Directory If not a media file then send null as parameter to getExternalFilesDir() method The DIRECTORY_ constants allow Android's Media Scanner to categorize files in the system External files associated with application are deleted when application uninstalled 24

External Data Shared Files If you want to save files to be shared with other apps: save the files (audio, images, video, etc.) to one of the public directories on the external storage device Environment.getExternalStoragePublicDirectory( Strint type) method returns a File object which is directory same types as getExternalFilesDir method 25

Examining Shared Directories Not the same as the system media directories 26

Result 27

SQLite Database Structured data stored in a private database More on this next lecture 28

Network Connection Store data on web with your own network server Use wireless or carrier network to store and retrieve data on web based server classes from java.net and android.net 29