DOC PREVIEW
CU-Boulder CSCI 5448 - ADVANCED ANDROID

This preview shows page 1-2-3-20-21-40-41-42 out of 42 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 42 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

© Kenneth M. Anderson, 2011ADVANCED ANDROIDCSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGNLECTURE 19 — 03/15/20111Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Goals of the LecturePresent more examples of the Android FrameworkPassing Information between ActivitiesReading and Writing Files2D Graphics and Touch EventsApplication PreferencesWorking with a Database2Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Passing InformationIn our examples so farwe’ve seen one activity launch another activitybut each activity has been independent of the otherLet’s see how one activity can send information to another activityWe’ll also take a look at storing data that persists between sessions of using the application3Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Profile ViewerOur application is going to show a list of user namesWe can choose to add and delete user namesWe can also edit a user to launch a new activity that will then display that user’s profileOur program will use Java serialization to persist a data structure that stores user names and profilesThe data structure will be a Map<String, ProfileData>We’ll discuss ProfileData in a moment4Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Java Serialization (I)Java serialization is a technology that can bothpersist a set of objects, andlater retrieve that set such that all objects are recreated and all connections between them are reestablishedjava.io provides two classes to help with thisObjectOutputStream and ObjectInputStreamYo u u s e t h e f o r m e r t o s av e a n d t h e l a t t e r t o l o a d5Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Java Serialization (II)Most Java types, including collections, can be serializedUser-defined types can also be serializedYo u n e e d t o i m p l e m e n t j av a . i o . S e r i a l i z a b l eAnd, you need to implement two methodsreadObject(java.io.ObjectInputStream stream)writeObject(java.io.ObjectOutputStream stream)6Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Java Serialization (III)In writeObject(), you place code that writes each internal attribute of your object on to the output streamIn readObject(), you place code that reads each attribute off of the input stream in the same order they were written by writeObjectThen, when it comes time for your class to be persisted, Java’s serialization framework will call readObject and writeObject as needed passing the appropriate IO stream7Tuesday, March 15, 2011© Kenneth M. Anderson, 2011ProfileData (I)For our Profile Viewer application, our ProfileData class stores a user’s first name, last name, and e-mail addressProfileData is implemented as a data holder with getter and setter methods for each attributeIt implements java.io.Serializable as neededIt also contains a serialVersionUID that was autogenerated by Eclipse that is used to add support for versioning. If we ever change the ProfileData class, we’ll need to update the UID.8Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Profile Data (II)Our writeObject Method looks like thisprivate void writeObject(java.io.ObjectOutputStream stream) throws IOException {!! stream.writeObject(firstName);!! stream.writeObject(lastName);!! stream.writeObject(email);}9Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Profile Data (III)Our readObject Method looks like thisprivate void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException {!! firstName = (String)stream.readObject();!! lastName = (String)stream.readObject();!! email = (String)stream.readObject();!}10Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Java Serialization (IV)Having configured ProfileData in this way, then the code to write a Map<String, ProfileData> data structure is:ObjectOutputStream output =!new ObjectOutputStream(new FileOutputStream(f));output.writeObject(profiles);Two l i n e s o f c o d e ! ( I g n o r i n g e x c e p t i o n h a n d l e r s )11Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Java Serialization (V)The code to read a Map<String, ProfileData> is:ObjectInputStream input =!new ObjectInputStream(new FileInputStream(f));profiles = (TreeMap<String,ProfileData>) input.readObject();Just two more lines of code!12Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Java Serialization (VI)Hiding in those two lines of code was a reference to a variable named “f”; Here’s the relevant part:new FileInputStream(f) or new FileOutputStream(f)In both cases, we were passing an instance of java.io.File to the IO streams to specify where our persistent data is storedSo, now we need to look at how we deal with files in Android13Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Dealing With Files (I)Each Android application has a directory on the file systemYo u c a n v e r i f y t h i s b y l a u n c h i n g a n e m u l a t o r a n d t h e n invoking the “adb shell” commandadb is stored in $ANDROID/tools (2.x) or $ANDROID/platform_tools (3.x)This command provides you with a command prompt to your device; recall that Android runs on linuxcd to data/data to see a list of application directories14Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Dealing With Files (II)For Profile Viewer, cd into the com.example.profileviewer directory (you’ll need to compile and install Profile Viewer onto your device first!)The directory contains two subdirectoriesfiles and libWhenever you ask for access to your application’s directory and create a file, it will be stored in the “files” subdirectoryApplication directories are nominally private; other apps can’t access them15Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Dealing With Files (III)Android provides several useful methods for accessing your application’s private directorygetFilesDir() - returns a java.io.File that points at the directoryfileList() - returns list of file names in app’s directoryopenFileInput() - returns FileInputStream for readingopenFileOutput() - returns FileOutputStream for writingdeleteFile() - deletes a file that is no longer needed16Tuesday, March 15, 2011© Kenneth M. Anderson, 2011Profile Viewer’s Use of FilesIn Profile Viewer, all we need to use is getFilesDir()We use that to create a java.io.File object that points at a file called “profiles.bin” in our app’s directoryWe then pass that file to our save/load methodsThat code looks like thisprofiles.load(new


View Full Document

CU-Boulder CSCI 5448 - ADVANCED ANDROID

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download ADVANCED ANDROID
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view ADVANCED ANDROID and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view ADVANCED ANDROID 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?