CS378 -Mobile Computing. Persistence

Similar documents
CS371m - Mobile Computing. Persistence

Android: Data Storage

Mobile and Ubiquitous Computing: Android Programming (part 4)

Spring Lecture 5 Lecturer: Omid Jafarinezhad

SAVING SIMPLE APPLICATION DATA

The Basis of Data. Steven R. Bagley

Mobile Application Programing: Android. Data Persistence

MOBILE APPLICATIONS PROGRAMMING

Data storage and exchange in Android

How to access your database from the development environment. Marco Ronchetti Università degli Studi di Trento

Eng. Jaffer M. El-Agha Android Programing Discussion Islamic University of Gaza. Data persistence

CSCU9YH: Development with Android

Mediensystemen mit Android

Andorid Storage Options

SharedPreferences Internal Storage External Storage SQLite databases

LẬP TRÌNH DI ĐỘNG. Bài 9: Lưu Trữ & SQLite

Android File & Storage

CS378 -Mobile Computing. Audio

Software Engineering Large Practical: Preferences, storage, and testing

07. Data Storage

Android Programming Lecture 16 11/4/2011

Lab 1 - Setting up the User s Profile UI

CS 193A. Activity state and preferences

Single Application Persistent Data Storage. CS 282 Principles of Operating Systems II Systems Programming for Android

Programming in Android. Nick Bopp

Mobile Application Development Android

Software Practice 3 Today s lecture Today s Task

Mobile Development Lecture 11: Activity State and Preferences

Data Management CS 4720 Mobile Application Development

Optimizing Your Android Applications

Software Engineering Large Practical: Storage, Settings and Layouts. Stephen Gilmore School of Informatics October 27, 2017

Basic UI elements: Defining Activity UI in the code. Marco Ronchetti Università degli Studi di Trento

How-to s and presentations. Be prepared to demo them in class. ons

Introduction to Programming Using Java (98-388)

Mr. Pritesh N. Patel Assistant Professor MCA ISTAR, V. V. Nagar ANDROID DATABASE TUTORIAL

Computer Science Large Practical: Storage, Settings and Layouts. Stephen Gilmore School of Informatics October 26, 2017

Web Server Project. Tom Kelliher, CS points, due May 4, 2011

Topics. Transient vs Persistent Representation of Objects. Object Serialization CSE Lecture 9 Inheritance, III

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Android Database Programming

Mobile Application Development Android

CS378 -Mobile Computing. More UI -Part 2

How to access your database from the development environment. Marco Ronchetti Università degli Studi di Trento

Data Conversion & Scanner Class

CS370 Operating Systems

Android Camera. Alexander Nelson October 6, University of Arkansas - Department of Computer Science and Computer Engineering

Files and Streams

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Presentation Outline 10/16/2016

2. The object-oriented paradigm

ADF Mobile Code Corner

Java Identifiers, Data Types & Variables

Special Topics: Programming Languages

Automatically persisted among application sessions

Mobile Application Development Android

Starting Out with Java: From Control Structures Through Objects Sixth Edition

CS378 - Mobile Computing. Anatomy of an Android App and the App Lifecycle

CS 11 java track: lecture 1

Android Data Storage

Saving application preferences

8/31/2015 BITS BYTES AND FILES. What is a bit. Representing a number. Technically, it s a change of voltage

1. What are the key components of Android Architecture? 2. What are the advantages of having an emulator within the Android environment?

CHAPTER 7 OBJECTS AND CLASSES

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

Orientation & Localization

CS378 -Mobile Computing. What's Next?

CHAPTER 7 OBJECTS AND CLASSES

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

First Programming Language in CS Education The Arguments for Scala


(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Android Programming in Bluetooth Cochlea Group

Kernels & Processes The Structure of the Operating System

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

SHWETANK KUMAR GUPTA Only For Education Purpose

Level 2: The Hardware World Chapters 4 and 5 (topics of other cs courses)

Announcements. Android: n-puzzle Walkthrough. Tommy MacWilliam. Dynamic GUIs. ListViews. Bitmaps. Gameplay. Saving State. Menus

Computational Expression

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

Android Jelly Bean Manual Install Application On Sd Card

Profiling & Optimization

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

13 th Windsor Regional Secondary School Computer Programming Competition

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Activities and Fragments

CS3600 SYSTEMS AND NETWORKS

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

CS 528 Mobile and Ubiquitous Computing Lecture 1b: Introduction to Android. Emmanuel Agu

Mobile Application Development. Introduction. Dr. Christelle Scharff Pace University, USA

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CS 4518 Mobile and Ubiquitous Computing Lecture 2: Introduction to Android. Emmanuel Agu

Syllabus- Java + Android. Java Fundamentals

CS 528 Mobile and Ubiquitous Computing Lecture 4a: Fragments, Camera Emmanuel Agu

CS113: Lecture 4. Topics: Functions. Function Activation Records

Why Study Assembly Language?

Required Core Java for Android application development

Working with Mediator Framework

Android System Architecture. Android Application Fundamentals. Applications in Android. Apps in the Android OS. Program Model 8/31/2015

Transcription:

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 SharedPreferencesClass 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 SharedPreferencesobject for application using these methods: getsharedpreferences(string name, int mode) if you want multiple files getpreferences(int mode) 6

SharedPreferencesModes 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 SharedPreferencesobject 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<String,?> 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 FileOutputStreamobject (regular Java class) Modes same as SharedPreferencesminus MODE_MULTI_PROCESS and addition of MODE_APPEND 14

Writing to Files FileOutputStreamwrites raw bytes arrays of bytes or single bytes Much easier to wrap the FileOutputStreamin PrintStreamobject 15

files saved to device data directory for app Reading from Files 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 InputStreamto 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 filesystemdirectory 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 worldreadable files may be modified by user when they enable USB mass storage for device 20

Call Checking Media Availability 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 nullas parameter to getexternalfilesdir() method The DIRECTORY_<TYPE> 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( Strinttype) 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