• Save
Android Programming: Lecture 07
Upcoming SlideShare
Loading in...5

Android Programming: Lecture 07



1) XML Parsing on Android...

1) XML Parsing on Android
2) Internet Access in Android Applications
3) Android Data Persistence through Files and SharedPreferences
4) Class home page is at http://vkedco.blogspot.com/2011/08/theory-of-computation-home.html



Total Views
Views on SlideShare
Embed Views



32 Embeds 773

http://vkedco.blogspot.com 504
http://vkedco.blogspot.in 116
http://vkedco.blogspot.fr 16
http://vkedco.blogspot.ca 15
http://vkedco.blogspot.co.uk 14
http://vkedco.blogspot.de 12
http://vkedco.blogspot.co.il 9
http://vkedco.blogspot.com.es 8
http://vkedco.blogspot.se 7
http://vkedco.blogspot.ie 7
http://vkedco.blogspot.pt 7
http://vkedco.blogspot.cz 6
http://vkedco.blogspot.ro 4
http://vkedco.blogspot.nl 4
http://vkedco.blogspot.tw 4
http://vkedco.blogspot.mx 4
http://vkedco.blogspot.com.br 4
http://vkedco.blogspot.jp 4
http://vkedco.blogspot.ch 3
http://vkedco.blogspot.gr 3
http://vkedco.blogspot.com.au 3
http://vkedco.blogspot.co.nz 3
http://vkedco.blogspot.sg 2
http://www.vkedco.blogspot.com 2
http://vkedco.blogspot.it 2
http://vkedco.blogspot.com.ar 2
http://vkedco.blogspot.hk 2
http://vkedco.blogspot.com.tr 2
http://vkedco.blogspot.fi 1
http://vkedco.blogspot.ru 1
http://vkedco.blogspot.co.at 1
http://vkedco.blogspot.kr 1



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    Android Programming: Lecture 07 Android Programming: Lecture 07 Presentation Transcript

    • Android Programming Lecture 07 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
    • Resources Source code and presentation materials for this lecture are available at http://vkedco.blogspot.com/2011/10/android-programming-lecture-07.htmlwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Outline ● XML Parsing on Android ● Internet Access in Android Apps ● Data Persistence through Files and Shared Preferenceswww.youtube.com/vkedco www.vkedco.blogspot.com
    • XML Parsing on Androidwww.youtube.com/vkedco www.vkedco.blogspot.com
    • XML Parsing on Android ● Java + XML = Portable Code + Portable Data ● Android platform strength is that it leverages Java ● Android SDK does not support everything available in JRE, but it does support a significant part of it, including most of Javas XML APIswww.youtube.com/vkedco www.vkedco.blogspot.com
    • SAX vs DOM ● Android supports both Javas Simple API for XML (SAX) and the Document Object Model (DOM) ● Android does not support Streaming API for XML (StAX) but does provide a functionally equivalent library ● To the developer, SAX and DOM on Android are the same as they are on JREwww.youtube.com/vkedco www.vkedco.blogspot.com
    • SAX vs DOM ● SAX does not read an XML document into memory ● SAX parses documents piece by piece through event handlers implemented by the programmer ● DOM does read an entire XML document into memory and then gives the programmer a set of methods to manipulate the document tree ● DOM tends to be easier to program than SAX ● Both SAX and DOM are useful tools: it is all about space/time tradeoffs and the needs of a specific applicationwww.youtube.com/vkedco www.vkedco.blogspot.com
    • DOMSamples Projectwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Things To Take Away from This Project ● Introduction to DOM on Android ● How to build a XML document with DOM ● How to parse an XML document with DOMwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Mathematicians.xml <?xml version="1.0" encoding="utf-8"?> <Mathematician> <!-- class of mathematicians --> <GeorgCantor research_area="set theory"> <!-- 1845 - 1918 --> </GeorgCantor> <DavidHilbert research_area="mathematical logic"> <!-- 1862 - 1943 --> </DavidHilbert> <GiuseppePeano research_area="number theory"> <!-- 1858 - 1932 --> </GiuseppePeano> </Mathematician>www.youtube.com/vkedco www.vkedco.blogspot.com
    • How Can One Generate Mathematicians.xml Programmatically with DOM?www.youtube.com/vkedco www.vkedco.blogspot.com
    • Generating the Documents Root Element import org.w3c.dom.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element mathematician = doc.createElement(res.getString(R.string.mathematician_tag)); Comment mathematician_comment = doc.createComment("class of mathematicians"); mathematician.appendChild(mathematician_comment); doc.appendChild(mathematician);www.youtube.com/vkedco
    • Generating the Roots Children // 1. Child element for Georg Cantor Element georg_kantor = doc.createElement(res.getString(R.string.georg_cantor_tag)); Comment georg_kantor_comment = doc.createComment("1845 - 1918"); georg_kantor.appendChild(georg_kantor_comment); georg_kantor.setAttribute(research_area_attrib, "set theory"); mathematician.appendChild(georg_kantor); // 2. Ghild element for Giuseppe Peano Element giuseppe_peano = doc.createElement(res.getString(R.string.giuseppe_peano_tag)); Comment giuseppe_peano_comment = doc.createComment("1858 - 1932"); giuseppe_peano.appendChild(giuseppe_peano_comment); giuseppe_peano.setAttribute(research_area_attrib, "number theory"); mathematician.appendChild(giuseppe_peano);www.youtube.com/vkedco www.vkedco.blogspot.com
    • XMLDocBuilder.buildDocOne() Methodwww.youtube.com/vkedco www.vkedco.blogspot.com
    • After Mathematicians.xml Is Generated, How Can One Manipulate It with DOM?www.youtube.com/vkedco www.vkedco.blogspot.com
    • Document Manipulation Element mathematician_elem = doc.getDocumentElement(); NodeList children = mathematician_elem.getChildNodes(); for(int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if ( child.getNodeType() == Node.COMMENT_NODE) { // process comment node } else if ( child.getNodeType() == Node.ELEMENT_NODE) { // process element node } else { // do something else }www.youtube.com/vkedco www.vkedco.blogspot.com
    • DOMSamplesAct.parseXMLDoc() Methodwww.youtube.com/vkedco www.vkedco.blogspot.com
    • XML Parsing on Android and Internet Accesswww.youtube.com/vkedco www.vkedco.blogspot.com
    • EarthquakeViewer Projectwww.youtube.com/vkedco www.vkedco.blogspot.com
    • What To Take Away From This Project ● XML Parsing with DOM ● Internet Access from Android Applicationswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Earthquake Viewer ● Create a list-based activity that connects to an earthquake RSS feed from USGS ● Parse the XML with the list of earthquakes all over the world ● Display the location, magnitude, and time of each earthquake it contains ● When the user clicks on an earthquake, an AlterDialog provides a prompt that includes a link to the USGS web sitewww.youtube.com/vkedco www.vkedco.blogspot.com
    • Earthquake Viewer Screenswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Web Resources <string name="quake_feed"> http://earthquake.usgs.gov/earthquakes/catalogs/1day-M2.5.xml </string> <string name="quake_feed_backup"> http://digital.cs.usu.edu/~vkulyukin/vkweb/earthquakes.xml </string>www.youtube.com/vkedco www.vkedco.blogspot.com
    • USGS XML File <?xml version="1.0"?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss"> <entry><id>urn:earthquake-usgs-gov:nn:00350821</id> <title>M 2.6, Nevada</title> <updated>2011-10-10T22:08:55Z</updated> <link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/nn00350821.php"/> <link rel="related" type="application/cap+xml" href="http://earthquake.usgs.gov/earthquakes/catalogs/cap/nn00350821" /> <georss:point>37.3562 -117.1253</georss:point> <georss:elev>-12000</georss:elev> </entry> </feed>www.youtube.com/vkedco www.vkedco.blogspot.com
    • EarthquakeViewer: main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/earthquakeListView" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>www.youtube.com/vkedco www.vkedco.blogspot.com
    • EarthquakeViewr Project: Quake.javawww.youtube.com/vkedco www.vkedco.blogspot.com
    • quake_details.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/quakeDetailsTextView" android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="14sp" android:autoLink="all" /> </LinearLayout>www.youtube.com/vkedco www.vkedco.blogspot.com
    • EarthquakeViewerAct.refreshEarthquakes() Methodwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Data Persistence Through Files and Shared Preferenceswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Data Persistence Techniques ● Local Files ● Shared Preferences ● Content Providers ● Cloud serverswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Local Files Back to DOMSamples Project: XMLDocBuilder.buildDocTwo() and /res/raw/mathematicians.xmlwww.youtube.com/vkedco www.vkedco.blogspot.com
    • SharedPreferences ● Lightweight key/value pair mechanism for saving primitive application data (Boolean, string, float, long, and integer) ● Commonly used for saving users application preferences, UI states, application settings ● Instances of SharedPreferences class can contain key/value pairs that can be shared among all activities running within the same application contextwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Typical Use: Storing Shared Preferences public static String PREFS = “MyPrefs”; SharedPreferences myPrefs = getSharedPreferences(PREFS, Activity.MODE_PRIVATE); SharedPreferences.Editor edit = myPrefs.edit(); edit.putBoolean(“True”, true); edit.putFloat(“Float”, 5f); edit.putInt(“Int”, 10); edit.putString(“StringKey”, “StringValue”);www.youtube.com/vkedco www.vkedco.blogspot.com
    • Typical Use: Retrieving Shared Preferences public static String PREFS = “MyPrefs”; SharedPreferences myPrefs = getSharedPreferences(PREFS, Activity.MODE_PRIVATE); boolean storedBool = myPrefs.getBoolean(“True”, false); float storedFloat = myPrefs.getFloat(“Float”, 0f); int storedInt = myPrefs.getInt(“Int”, 0); String storedStr = myPrefs.getString(“StringKey”, “”);www.youtube.com/vkedco www.vkedco.blogspot.com
    • SharedPrefs1 Projectwww.youtube.com/vkedco www.vkedco.blogspot.com
    • What To Take Away From This Project ● Saving SharedPreferences ● Loading SharedPreferenceswww.youtube.com/vkedco www.vkedco.blogspot.com
    • SharedPreferences1 Project: Screenswww.youtube.com/vkedco www.vkedco.blogspot.com
    • SharedPrefs1 Project: SharedPrefs1Act.safePrefs() and SharedPrefs1Act.loadPrefs()www.youtube.com/vkedco www.vkedco.blogspot.com
    • Back to EarthquakeViewer Project: Adding Preferenceswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Adding Preferenceswww.youtube.com/vkedco www.vkedco.blogspot.com
    • XML Layout: preferences.xmlwww.youtube.com/vkedco www.vkedco.blogspot.com
    • Populating Preference Spinners private void populateSpinners() { ArrayAdapter<CharSequence> freqAdapter; freqAdapter = ArrayAdapter.createFromResource(this, R.array.update_freq_options, android.R.layout.simple_spinner_item); int spinner_dd_item = android.R.layout.simple_spinner_dropdown_item; freqAdapter.setDropDownViewResource(spinner_dd_item); updateFreqSpinner.setAdapter(freqAdapter); ArrayAdapter<CharSequence> magnAdapter; magnAdapter = ArrayAdapter.createFromResource(this, R.array.magnitude_options, android.R.layout.simple_spinner_item); magnAdapter.setDropDownViewResource(spinner_dd_item); magnitudeSpinner.setAdapter(magnAdapter); }www.youtube.com/vkedco www.vkedco.blogspot.com
    • PreverencesAct.updateUIFromPreferences() and PreferencesAct.savePreferences()www.youtube.com/vkedco www.vkedco.blogspot.com
    • References & Reading Suggestions ● http://developer.android.com/reference/javax/xml/parsers/package-summary.html ● Chapters 5, 6 in “Professional Android 2 Application Development” by Rito Meier ● Chapters 2, 3 in “Java and XML” by Brett McLaughlin ● Two links on serialization on Android (many thanks to Jared Barneck for sending these to me): – http://simple.sourceforge.net – http://simple.sourceforge.networkswww.youtube.com/vkedco www.vkedco.blogspot.com
    • Feedback Bugs, errors to vladimir dot kulyukin at gmail dot comwww.youtube.com/vkedco www.vkedco.blogspot.com