Mediensystemen mit Android

Similar documents
Mobile and Ubiquitous Computing: Android Programming (part 4)

The Basis of Data. Steven R. Bagley

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

CS371m - Mobile Computing. Persistence

SAVING SIMPLE APPLICATION DATA

CS378 -Mobile Computing. Persistence

07. Data Storage

Android: Data Storage

CSCU9YH: Development with Android

SharedPreferences Internal Storage External Storage SQLite databases

Android Programming Lecture 16 11/4/2011

Software Engineering Large Practical: Preferences, storage, and testing

MOBILE APPLICATIONS PROGRAMMING

Data storage and exchange in Android

Atmiya Institute of Technology & Science, Rajkot Department of MCA

Lab IV. Transaction Management. Database Laboratory

Mobile Application Development Android

Praktikum Mobile Productivity

Mobile Application Development Android

Praktikum Entwicklung von Mediensystemen mit Android

Android Components. Android Smartphone Programming. Matthias Keil. University of Freiburg

An Android Studio SQLite Database Tutorial

Traffic violations revisited

SharedPreference. <map> <int name="access_count" value="3" /> </map>

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

SQLite. 5COSC005W MOBILE APPLICATION DEVELOPMENT Lecture 6: Working with Databases. What is a Database Server. Advantages of SQLite

Android Components Android Smartphone Programming. Outline University of Freiburg. Data Storage Database University of Freiburg. Notizen.

DATABASE SYSTEMS. Database programming in a web environment. Database System Course, 2016

STARCOUNTER. Technical Overview

Android Programming Lecture 15 11/2/2011

Sqlite Update Failed With Error Code 19 Android

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

SQLite Database. References. Overview. Structured Databases

Automatically persisted among application sessions

Chapter 8: Working With Databases & Tables

Part I: Stored Procedures. Introduction to SQL Programming Techniques. CSC 375, Fall 2017

Data Structures. 03 Streams & File I/O

Revising CS-M41. Oliver Kullmann Computer Science Department Swansea University. Robert Recorde room Swansea, December 13, 2013.

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

Fun facts about recursion

Android Application Development Course 28 Contact Hours

CS221 Lecture: Java Database Connectivity (JDBC)

Introduction to pysqlite

Mobile Application Development Android

Lecture 7: Data Persistence : shared preferences. Lecturer : Ali Kadhim Al-Bermani Mobile Fundamentals and Programming

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

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

e-pg Pathshala Subject: Computer Science Paper: Web Technology Module: JDBC INTRODUCTION Module No: CS/WT/26 Quadrant 2 e-text

Exceptions and Design

Creating the Data Layer

Database Development In Android Applications

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Database Application Development

Object Persistence Design Guidelines

Presentation Outline 10/16/2016

Mobile Application Programing: Android. Data Persistence

App Development for Smart Devices. Lec #5: Content Provider

DATABASE SYSTEMS. Database programming in a web environment. Database System Course,

Discuss setting up JDBC connectivity. Demonstrate a JDBC program Discuss and demonstrate methods associated with JDBC connectivity

Performance and Forgiveness. June 23, 2008 Margo Seltzer Harvard University School of Engineering and Applied Sciences

CSE 530A ACID. Washington University Fall 2013

Student Number: Please fill out the identification section above as well as the one on the back page, and read the instructions below. Good Luck!

INTRODUCTION TO JDBC - Revised spring

Java E-Commerce Martin Cooke,

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

Implement a Multi-Frontend Chat Application based on Eclipse Scout

CS 3 Introduction to Software Engineering. 3: Exceptions

Server-side Web Programming

Spring Lecture 5 Lecturer: Omid Jafarinezhad

Chapter 16: Databases

Kyle Brown Knowledge Systems Corporation by Kyle Brown and Knowledge Systems Corporation

CSE 143 Lecture 25. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides adapted from Marty Stepp

In This Lecture. Transactions and Recovery. Transactions. Transactions. Isolation and Durability. Atomicity and Consistency. Transactions Recovery

CSE 143 Lecture 21. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides created by Marty Stepp

CSE 143 Lecture 22. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides created by Marty Stepp

SQL in a Server Environment (ii)

CSCD43: Database Systems Technology. Lecture 4

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

ECE 650 Systems Programming & Engineering. Spring 2018

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

Praktikum Entwicklung von Mediensystemen mit Android Introduction to Android

Are Code Examples on an Online Q&A Forum Reliable?

PIC 20A Streams and I/O

Enhancement of Open Source Monitoring Tool for Small Footprint Databases

CS 250 SQL and SQLite. Benjamin Dicken

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

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

Revising CS-M41. Oliver Kullmann Computer Science Department Swansea University. Linux Lab Swansea, December 13, 2011.

Transaction Management: Concurrency Control, part 2

Locking for B+ Trees. Transaction Management: Concurrency Control, part 2. Locking for B+ Trees (contd.) Locking vs. Latching

October/November 2009

My name is Brian Pottle. I will be your guide for the next 45 minutes of interactive lectures and review on this lesson.

Introduction to Data Management CSE 344

Connect Databases to AutoCAD with dbconnect Nate Bartley Test Development Engineer autodesk, inc.

Lightweight J2EE Framework

CSE593 Transaction Processing 1/10/01. This document replaces the preliminary project description that was handed out on January 3.

Tiers (or layers) Separation of concerns

Transactions. Silberschatz, Korth and Sudarshan

Transcription:

LFE Medieninformatik i ik Prof. Dr. Heinrich i Hußmann (Dozent), Alexander De Luca, Gregor Broll, Max-Emanuel Maurer (supervisors) Praktikum Entwicklung von Mediensystemen mit Android Storing, Retrieving and Exposing Data

Outline Introduction Lightweight i h Storing Files Databases Network Content Providers Exercise 3

Introduction All application data are private to an application Mechanisms to make data available for other applications Some simple/basic applications do not require information to be stored More elaborated software needs storage/retrieval functionality for different functionalities like: Preserving gan application s a status au (paused, first startup, etc.) Saving user preferences (font size, sound on/off, etc.) Working with complex data structures (calendars, maps, etc.)

Purpose & Resource Introduction Depending on the purpose of storing data, Android offers approaches with different complexity: Store and retrieve simple name/value pairs File operations (read, write, create, delete, etc.) SQLite databases to work with complex data structures Network operations to store and retrieve data from a network Content providers to read/write data from an application s private data

Preferences Application preferences are simple name/value pairs like greeting=hello name or sound = off To work with preferences, Android offers an extremely simple approach Preferences can only be shared with other components in the same package Preferences cannot be shared across packages Private preferences will not be shared at all

Using Preferences Preferences Reading Preferences Context.getSharedPreferences(String t tsh name, int mode) opens a set of preferences defined by name If a name is assigned, the preferences set will be shared amongst the components of the same package Activity.getPreferences(int mode) can be used to open a set that is private to the calling activity Opens a preferences set with the name Preferences in private mode SharedPreferences settings = getsharedpreferences("preferences", MODE_PRIVATE); boolean sound = settings.getboolean("sound", false); Reads a boolean parameter from the set. If the parameter does not exist, it will be created with the value defined in the second attribute. (other functions: getall(), getint(), getstring(), etc.)

Using Preferences Preferences Writing Preferences Changes on preferences are done using an Editor (SharedPreferences.Editor) object Each setting has one global Editor instance to administrate changes Consequence: each change will be available to every activity working with that preferences set Gets the Editor instance of the preferences set SharedPreferences.Editor editor = settings.edit(); editor.putboolean("sound", false); // COMMIT!! editor.commit(); Writes a boolean to a parameter Attention: Changes are not drawn back to the settings before the commit is performed

Files Files can be used to store bigger amounts of data then using preferences Android offers functionality to read/write files Only local files can be accessed Advantage: can store huge amounts of data Disadvantage: file update or changing in the format might result in huge programming effort

Working with Files Files Reading from files Context.openFileInput(String t t(st i name) opens a FileInputStream of a private file associated with the application Throws a FileNotFoundException if the file doesn t exist Open the file test2.txt (can be any name) FileInputStream in = this.openfileinput("test2.txt"); in.close(); Don t forget to close the InputStream at the end

Working with Files Files Writing files Context.openFileOutput(String t t t(st i name, int mode) opens a FileOutputStream of a private file associated with the application If the file does not exist, it will be created FileOutputStreams can be opened in append mode, which means that new data will be added at the end of the file Open the file test2.txt for writing (can be any name) FileOutputStream out = this.openfileoutput("test2.txt", MODE_APPEND); in.close(); Using MODE-APPEND opens the file in append mode Don t forget to close the InputStream at the end

Working with Files Files Reading static files To open static ti files packed in the application, use Resources.openRawResource (R.raw.mydatafile) The files have to be put in the folder res/raw/ / Get the contexts resources InputStream in = this.getresources().openrawresource(r.raw.test); in.close(); Don t forget to close the InputStream at the end

SQLite Databases In some cases, files are not efficient If multi-threaded th d data access is relevant If the application is dealing with complex data structures that might change Etc. Therefore, Android comes with built-in SQLite support Databases are private to the package that created them Support for complex data types, e.g. contact information (first name, familiy name, address, ) Databases should not be used to store files Hint: an example on how to use databases can be found in the SDK at samples/notepad

SQLite Databases SQLite is a lightweight software library Implements a fully ACID-compliant database Atomicity Consistency Isolation Durability Size only several kilobytes Some SQL statements are only partially supported (e.g. ALTER TABLE) See http://www.sqlite.org/ for more information

Using Databases SQLite Databases Creating a database Context.createDatabase(String t t t name, int version, int mode, CursorFactory factory) creates a new database and returns a SQLiteDatabase object Throws a FileNotFoundException if the database could not be created Create a database with the name test.db (can be any name) SQLiteDatabase dbase = this.createdatabase("test.db", 1, MODE_PRIVATE, null); Optional CursorFactory parameter

Using Databases SQLite Databases Deleting a database Context. t deletedatabase(string t name) deletes the database with the specified name Returns true if the database was successfully deleted or false if not (e.g. database does not exist) Delete database test.db boolean success = this.deletedatabase("test.db);

Using Databases SQLite Databases Opening a database Context.openDatabase(String t t file, CursorFactory factory) opens an existing database and returns a SQLiteDatabase object Throws a FileNotFoundException if the database does not exist yet Create a database with the name test.db (can be any name) SQLiteDatabase dbase = this.opendatabase("test.db", null); dbase.close(); Don t forget to close the database at the end Optional CursorFactory parameter

Using Databases SQLite Databases Non-Query SQL Statements SQLiteDatabase.execSQL(String t SQL(St i sql) can be used to execute non-query SQL statements, that is statements without a result Includes CREATE TABLE, DROP TABLE, INSERT etc. Examples: Create a table with the name test and two parameters dbase.execsql("create TABLE test (_id INTEGER PRIMARY KEY, somenumber INTEGER);"); Insert a tuple into the database dbase.execsql("insert into test (_id, somenumber) values(1,8);"); Drop the table test dbase.execsql("drop TABLE test");

Using Databases SQLite Databases Query SQL Statements - Cursors Android uses cursors to navigate through h query results Cursors are represented by the object android.database.cursor A cursor is simply a pointer that jumps from one tuple of the query s result to the next (or the previous or the first or ) The cursor returns the data of the tuple it is located at the moment Table test _id somenumber 1 8 2 10 3 2

Using Databases SQLite Databases To create a cursor, a query has to be executed either by SQL using rawquery() or by more elaborated methods like query() Cursor cur = dbase.rawquery("select * FROM test", null); if (cur!= null) { Attributes are retrieved int numcolumn = cur.getcolumnindex("somenumber"); with their index if (cur.first()) { do { Cursor offers different methods to int num = cur.getint(numcolumn); retrieve different datatypes like do something with it getint(int index) getstring(int index) } while (cur.next()); etc } } next() moves the cursor to the next row. It returns false if no more row is available. Other possible moves are previous() and first()

Using the IDE to Check Files and Databases The Android plug-in provides a view to check all created files and databases 1. Add File Explorer view to the IDE a) click c) click b) click

Using the IDE to Check Files and Databases 2. Check Files and Databases at /data/data/<package_name>/files databases click Th lti t f th t The ultimate proof that Android accepts ANY file and database name

Network Access Android also supports network access to access files remotely (through the network) Two major packages: java.net.* contains the standard Java network APIs android.net. net * adds additional helper classes to the standard Java APIs

Content Providers All preferences, files and databases created by an Android application are private To share data with other applications, an application has to create a Content Provider To retrieve data of another application its content provider has to be called Androids native Content Providers include: CallLog: information o about placed and received calls Settings.System: system settings and preferences

Exercise Chat-history Application Based on exercise 2 Functionality changing the status (available etc.) is stored and automatically set on starting the application the chat history has to be stored automatically a o each message has to be stored together with a timestamp two buttons to display the history o of the day o of all chat sessions any storing mechanism is ok SHOW HISTORY: DAY ALL Any improvements on the design or additional functionality is encouraged

See you next meeting!