Your SlideShare is downloading. ×
0
Android Storage Mobile Computing Bruce Scharlau, University of Aberdeen, 2009 Based on  android-sdk_1.6-r1 Unless otherwis...
Proxy issues while in eduroam <ul><li>Change settings programmatically as noted in practical pages </li></ul>Bruce Scharla...
There are several options for storage of data with Android <ul><li>We can put data into a preferences file. </li></ul><ul>...
Preference files are a  light-weight option <ul><li>We can put data into a preferences file. </li></ul><ul><li>Call Contex...
We can write larger data to file <ul><li>You can either write to a new file, or to a pre-included file under res/raw/mydat...
File writing is similar to what you’ve done before Bruce Scharlau, University of Aberdeen, 2009 Source: unlocking android,...
We can place data elsewhere on the network <ul><li>Use a web service to store data elsewhere as we saw last week. </li></u...
We can also persist data to a db <ul><li>Android API uses the built-in SQLite db.  </li></ul><ul><li>Each db is private to...
Android Notepad tutorial uses database Bruce Scharlau, University of Aberdeen, 2009 Useful db helper class for access and ...
Context Menu is special <ul><li>Acquire context menu by holding down selection key, which then pops up context menu </li><...
Unlocking Android db example covers more complex example Bruce Scharlau, University of Aberdeen, 2009 Stores locations to ...
Unlocking Android app uses db helper classes with sql Bruce Scharlau, University of Aberdeen, 2009 public static class Loc...
Unlocking Android app maps objects to sql for ease Bruce Scharlau, University of Aberdeen, 2009 public void insert(final L...
SQLite provides advanced db features <ul><li>There is transaction support </li></ul><ul><li>You can use prepared statement...
Can map objects to db <ul><li>Can read items from network as xml and convert to objects, which map to db </li></ul><ul><li...
Summary <ul><li>Can use preferences for each app </li></ul><ul><li>Can write/read files as with Java </li></ul><ul><li>Can...
Upcoming SlideShare
Loading in...5
×

Databases and other options

235

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
235
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Databases and other options"

  1. 1. Android Storage Mobile Computing Bruce Scharlau, University of Aberdeen, 2009 Based on android-sdk_1.6-r1 Unless otherwise stated, images are from android sdk See Unlocking Android (Ch 5) for more details
  2. 2. Proxy issues while in eduroam <ul><li>Change settings programmatically as noted in practical pages </li></ul>Bruce Scharlau, University of Aberdeen, 2009 Add code to app to handle Desired web service
  3. 3. There are several options for storage of data with Android <ul><li>We can put data into a preferences file. </li></ul><ul><li>We can put data into a ‘normal’ file. </li></ul><ul><li>We can send data across the network to a service. </li></ul><ul><li>We can use a database. </li></ul>Bruce Scharlau, University of Aberdeen, 2009
  4. 4. Preference files are a light-weight option <ul><li>We can put data into a preferences file. </li></ul><ul><li>Call Context.getSharedPreferences() to read and write values as key-value pairs. </li></ul><ul><li>Use Activity.getPreferences() with no name to keep them private to the calling activity </li></ul><ul><li>These are not sharable across applications, unless you expose them as a ‘content provider’. </li></ul>Bruce Scharlau, University of Aberdeen, 2009
  5. 5. We can write larger data to file <ul><li>You can either write to a new file, or to a pre-included file under res/raw/mydata </li></ul><ul><li>To can read data from a file, call Context.openFileInput() and pass it the local name and path of the file. It returns a standard Java FileInputStream object. </li></ul><ul><li>To write to a file, call Context.openFileOutput() with the name and path. It returns a FileOutputStream object. </li></ul>Bruce Scharlau, University of Aberdeen, 2009 You can only access files available to the application
  6. 6. File writing is similar to what you’ve done before Bruce Scharlau, University of Aberdeen, 2009 Source: unlocking android, p 159
  7. 7. We can place data elsewhere on the network <ul><li>Use a web service to store data elsewhere as we saw last week. </li></ul><ul><li>Can make this automatic, or at user discretion. (twitter apps, or photo capture) </li></ul>Bruce Scharlau, University of Aberdeen, 2009
  8. 8. We can also persist data to a db <ul><li>Android API uses the built-in SQLite db. </li></ul><ul><li>Each db is private to the application. In principle you could expose the data, if you expose the application as a content provider. </li></ul><ul><li>All databases, SQLite and others, are stored on the device in /data/data/package_name/databases. </li></ul>Bruce Scharlau, University of Aberdeen, 2009
  9. 9. Android Notepad tutorial uses database Bruce Scharlau, University of Aberdeen, 2009 Useful db helper class for access and crud details
  10. 10. Context Menu is special <ul><li>Acquire context menu by holding down selection key, which then pops up context menu </li></ul>Bruce Scharlau, University of Aberdeen, 2009
  11. 11. Unlocking Android db example covers more complex example Bruce Scharlau, University of Aberdeen, 2009 Stores locations to database within application as objects
  12. 12. Unlocking Android app uses db helper classes with sql Bruce Scharlau, University of Aberdeen, 2009 public static class Location { public long id; public long lastalert; public int alertenabled; public String zip; // include city and region because geocode is expensive public String city; public String region; public Location() { } public Location(final long id, final long lastalert, final int alertenabled, final String zip, final String city, final String region) { this.id = id; this.lastalert = lastalert; this.alertenabled = alertenabled; this.zip = zip; this.city = city; this.region = region; } Source: unlocking android, code Part of DBHelper class showing Location object Class also holds crud details to map object to sql
  13. 13. Unlocking Android app maps objects to sql for ease Bruce Scharlau, University of Aberdeen, 2009 public void insert(final Location location) { ContentValues values = new ContentValues(); values.put(&quot;zip&quot;, location.zip); values.put(&quot;city&quot;, location.city); values.put(&quot;region&quot;, location.region); values.put(&quot;lastalert&quot;, location.lastalert); values.put(&quot;alertenabled&quot;, location.alertenabled); this.db.insert(DBHelper.DB_TABLE, null, values); } public void update(final Location location) { ContentValues values = new ContentValues(); values.put(&quot;zip&quot;, location.zip); values.put(&quot;city&quot;, location.city); values.put(&quot;region&quot;, location.region); values.put(&quot;lastalert&quot;, location.lastalert); values.put(&quot;alertenabled&quot;, location.alertenabled); this.db.update(DBHelper.DB_TABLE, values, &quot;_id=&quot; + location.id, null); } Source: unlocking android, code Mapping makes coding easier
  14. 14. SQLite provides advanced db features <ul><li>There is transaction support </li></ul><ul><li>You can use prepared statements based on java.sql and set items as have done before – faster and more secure </li></ul><ul><li>You have a cursor to keep track of location within a resultset </li></ul>Bruce Scharlau, University of Aberdeen, 2009
  15. 15. Can map objects to db <ul><li>Can read items from network as xml and convert to objects, which map to db </li></ul><ul><li>Enables off network use and can sync later when connected </li></ul><ul><li>Might be pushing limits of device though with extra classes and memory usage </li></ul>Bruce Scharlau, University of Aberdeen, 2009
  16. 16. Summary <ul><li>Can use preferences for each app </li></ul><ul><li>Can write/read files as with Java </li></ul><ul><li>Can persist/read items over network (when available) </li></ul><ul><li>Can use SQLite one db per app </li></ul>Bruce Scharlau, University of Aberdeen, 2009
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×