Busy Android Developer's Guide to Peristence Ted Neward Neward & Associates http://www.tedneward.com | ted@tedneward.com
Credentials <ul><li>Who is this guy? </li></ul><ul><ul><li>Architectural Consultant, Neudesic Software </li></ul></ul><ul>...
Preferences <ul><li>Android SharedPreferences </li></ul><ul><ul><li>essentially, an XML file stored in your app's data are...
Preferences <ul><li>public class Calc extends Activity { </li></ul><ul><li>public static final String PREFS_NAME = &quot;M...
File I/O <ul><li>Android exposes the filesystem to standard Java I/O </li></ul><ul><ul><li>Two major categories of storage...
File I/O <ul><li>&quot;Internal storage&quot;: storage permanent to the device </li></ul><ul><ul><li>use openFileOutput() ...
File I/O <ul><li>&quot;Cached&quot; data is a little different </li></ul><ul><ul><li>getCacheDir() returns a File to the &...
File I/O <ul><li>&quot;External storage&quot;: removable storage (SD cards) </li></ul><ul><ul><li>API 7 and earlier </li><...
File I/O <ul><li>&quot;External storage&quot;: removable storage (SD cards) </li></ul><ul><ul><li>API 8 and later </li></u...
SQLite <ul><li>SQLite is a small-scale footprint RDBMS </li></ul><ul><ul><li>&quot;.. a software library that implements a...
SQLite <ul><li>Android supports SQLite (android.database.sqlite) </li></ul><ul><ul><li>SQLiteDatabase: core class for all ...
SQLite <ul><li>Android convenience classes </li></ul><ul><ul><li>SQLiteOpenHelper: extend this to receive &quot;events&quo...
SQLite Android SDK ships with sqlite3 tool <ul><ul><li>SQL console to SQLite files on device </li></ul></ul><ul><ul><li>ge...
db4o <ul><li>db4o is a small-footprintobject database </li></ul><ul><ul><li>http://www.db4o.com </li></ul></ul><ul><ul><li...
db4o Opening the database <ul><li>ObjectContainer db = null; </li></ul><ul><li>try { </li></ul><ul><li>db = Db4o.openFile(...
db4o Storing an object <ul><li>// Create the new Person </li></ul><ul><li>Person p = new Person(&quot;Matthew&quot;, &quot...
db4o Fetching an object (prototype-based) <ul><li>// Fetch all the Persons </li></ul><ul><li>ObjectSet persons =  </li></u...
db4o Updating an object <ul><li>// Fetch Matthew Neward (note the absence of error-checking) </li></ul><ul><li>Person matt...
db4o <ul><li>db4o provides more functionality than just what's shown here </li></ul><ul><ul><li>Native queries </li></ul><...
Wrapping up <ul><li>&quot;What do you know?&quot; </li></ul>
Summary <ul><li>Android provides a local-storage option </li></ul><ul><ul><li>... that offers the traditional storage opti...
Resources <ul><li>Busy Coder's Guide to Android (and more) </li></ul><ul><ul><li>Mark Murphy, http://www.commonsware.com <...
Upcoming SlideShare
Loading in...5
×

Android | Busy Java Developers Guide to Android: Persistence | Ted Neward

1,983

Published on

2011-11-02 | 10:00 AM - 11:00 AM
The Android ecosystem offers a few different ways to store things across restarts, but because Android also runs Java, a few more options also make themselves available, which means the Android developer has a pretty wide assortment of choices available to her. In this session, we’ll talk about those available choices, the pros and cons of each, and how to and when to use them. (Note: this session assumes you have some familiarity with the Android environment.)

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,983
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Android | Busy Java Developers Guide to Android: Persistence | Ted Neward

  1. 1. Busy Android Developer's Guide to Peristence Ted Neward Neward & Associates http://www.tedneward.com | ted@tedneward.com
  2. 2. Credentials <ul><li>Who is this guy? </li></ul><ul><ul><li>Architectural Consultant, Neudesic Software </li></ul></ul><ul><ul><li>Principal, Architect, Consultant and Mentor </li></ul></ul><ul><ul><ul><li>ask me how I can help your project or your team </li></ul></ul></ul><ul><ul><li>Microsoft MVP (F#, C#, Architect) </li></ul></ul><ul><ul><li>JSR 175, 277 Expert Group Member </li></ul></ul><ul><ul><li>Author </li></ul></ul><ul><ul><ul><li>Professional F# 2.0 (w/Erickson, et al; Wrox, 2010) </li></ul></ul></ul><ul><ul><ul><li>Effective Enterprise Java (Addison-Wesley, 2004) </li></ul></ul></ul><ul><ul><ul><li>C# In a Nutshell (w/Drayton, et all; OReilly, 2003) </li></ul></ul></ul><ul><ul><ul><li>SSCLI Essentials (w/Stutz, et al; OReilly, 2003) </li></ul></ul></ul><ul><ul><ul><li>Server-Based Java Programming (Manning, 2000) </li></ul></ul></ul><ul><ul><li>Blog: http://blogs.tedneward.com </li></ul></ul><ul><ul><li>Papers: http://www.tedneward.com/writings </li></ul></ul><ul><ul><li>Twitter: @tedneward </li></ul></ul>
  3. 3. Preferences <ul><li>Android SharedPreferences </li></ul><ul><ul><li>essentially, an XML file stored in your app's data area </li></ul></ul><ul><ul><ul><li>as such, it's probably the easiest to use ... </li></ul></ul></ul><ul><ul><ul><li>... but it has the usual limitations of XML </li></ul></ul></ul><ul><ul><li>usage is pretty straightforward </li></ul></ul><ul><ul><ul><li>Activity.getPreferences() to return a SharedPreferences object </li></ul></ul></ul><ul><ul><ul><li>MODE_PRIVATE makes the prefs invisible to all but this app </li></ul></ul></ul><ul><ul><ul><li>MODE_WORLD_READABLE/WRITEABLE allows other apps access </li></ul></ul></ul><ul><ul><ul><li>to view existing data, call get*() </li></ul></ul></ul><ul><ul><ul><li>to change, call edit() first, then put*()s, then commit() to write </li></ul></ul></ul>
  4. 4. Preferences <ul><li>public class Calc extends Activity { </li></ul><ul><li>public static final String PREFS_NAME = &quot;MyPrefsFile&quot;; </li></ul><ul><li>@Override </li></ul><ul><li>protected void onCreate(Bundle state){ </li></ul><ul><li>super.onCreate(state); </li></ul><ul><li>. . . </li></ul><ul><li>// Restore preferences </li></ul><ul><li>SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); </li></ul><ul><li>boolean silent = settings.getBoolean(&quot;silentMode&quot;, false); </li></ul><ul><li>setSilent(silent); </li></ul><ul><li>} </li></ul><ul><li>@Override </li></ul><ul><li>protected void onStop(){ </li></ul><ul><li>super.onStop(); </li></ul><ul><li>SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); </li></ul><ul><li>settings.edit().putBoolean(&quot;silentMode&quot;, true).commit(); </li></ul><ul><li>// ... or break it up into three steps if you prefer </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  5. 5. File I/O <ul><li>Android exposes the filesystem to standard Java I/O </li></ul><ul><ul><li>Two major categories of storage: </li></ul></ul><ul><ul><ul><li>internal storage </li></ul></ul></ul><ul><ul><ul><li>... and external storage (which may or may not be actually removable) </li></ul></ul></ul><ul><ul><li>Beyond where the files are stored, there's no real difference </li></ul></ul><ul><ul><ul><li>except that external storage can disappear at any time, isn't secure, and can be read over the USB cable </li></ul></ul></ul><ul><ul><li>Any sort of file storage is possible from here </li></ul></ul><ul><ul><ul><li>so anything that takes a File is a candidate for use </li></ul></ul></ul>
  6. 6. File I/O <ul><li>&quot;Internal storage&quot;: storage permanent to the device </li></ul><ul><ul><li>use openFileOutput() to return a java.io.FileOutputStream </li></ul></ul><ul><ul><li>use openFileInput() for a java.io.FileInputStream </li></ul></ul><ul><ul><ul><li>both use MODE_PRIVATE, MODE_APPEND, MODE_WORLD_READABLE/WRITEABLE </li></ul></ul></ul><ul><ul><li>getFilesDir() returns File for app's internal storage directory </li></ul></ul><ul><ul><li>deleteFile(), fileList() </li></ul></ul>
  7. 7. File I/O <ul><li>&quot;Cached&quot; data is a little different </li></ul><ul><ul><li>getCacheDir() returns a File to the &quot;cache&quot; directory </li></ul></ul><ul><ul><li>Android feels free to delete this data in low-storage scenarios </li></ul></ul><ul><ul><ul><li>but Android docs suggest not to rely on this; police yourself </li></ul></ul></ul>
  8. 8. File I/O <ul><li>&quot;External storage&quot;: removable storage (SD cards) </li></ul><ul><ul><li>API 7 and earlier </li></ul></ul><ul><ul><ul><li>getExternalStorageState() tells us SD card state </li></ul></ul></ul><ul><ul><ul><li>getExternalStorageDirectory() returns a java.io.File for the directory </li></ul></ul></ul><ul><ul><li>Files that should be shared go in a slightly different place </li></ul></ul><ul><ul><ul><li>from getExternalStorageDirectory(), store to Music/, Podcasts/, Ringtones/, Alarms/, Notifications/, Pictures/, Movies/, or Downloads/ </li></ul></ul></ul>
  9. 9. File I/O <ul><li>&quot;External storage&quot;: removable storage (SD cards) </li></ul><ul><ul><li>API 8 and later </li></ul></ul><ul><ul><ul><li>getExternalStorageState() tells us SD card state </li></ul></ul></ul><ul><ul><ul><li>getExternalFilesDir() returns a java.io.File for the directory </li></ul></ul></ul><ul><ul><li>Files that should be sharde go in a slightly different place </li></ul></ul><ul><ul><ul><li>getExternalStoragePublicDirectory(), passing in DIRECTORY_MUSIC, DIRECTORY_RINGTONES, etc </li></ul></ul></ul>
  10. 10. SQLite <ul><li>SQLite is a small-scale footprint RDBMS </li></ul><ul><ul><li>&quot;.. a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.&quot; </li></ul></ul><ul><ul><ul><li>http://www.sqlite.org </li></ul></ul></ul><ul><ul><li>Mostly SQL-92 compliant, but there are a few SQLisms unsupported: </li></ul></ul><ul><ul><ul><li>RIGHT and FULL OUTER JOIN </li></ul></ul></ul><ul><ul><ul><li>Complete ALTER TABLE support </li></ul></ul></ul><ul><ul><ul><li>Complete trigger support </li></ul></ul></ul><ul><ul><ul><li>Writing to VIEWs </li></ul></ul></ul><ul><ul><ul><li>GRANT and REVOKE </li></ul></ul></ul><ul><ul><li>Storage is single-file database, stored locally </li></ul></ul>
  11. 11. SQLite <ul><li>Android supports SQLite (android.database.sqlite) </li></ul><ul><ul><li>SQLiteDatabase: core class for all things SQLite </li></ul></ul><ul><ul><ul><li>openDatabase() or openOrCreateDatabase() </li></ul></ul></ul><ul><ul><ul><li>beginTransaction(), endTransaction() </li></ul></ul></ul><ul><ul><ul><li>insert(), delete(), update(), replace(), query() </li></ul></ul></ul><ul><ul><ul><li>execSQL() </li></ul></ul></ul><ul><ul><li>queries return either a &quot;projection&quot; (String[]) or a Cursor object </li></ul></ul><ul><ul><ul><li>either can generally be bound to a View Adapter </li></ul></ul></ul><ul><ul><ul><li>... or you can manually iterate/bind </li></ul></ul></ul>
  12. 12. SQLite <ul><li>Android convenience classes </li></ul><ul><ul><li>SQLiteOpenHelper: extend this to receive &quot;events&quot; about the database </li></ul></ul><ul><ul><ul><li>onCreate() </li></ul></ul></ul><ul><ul><ul><li>onUpgrade() </li></ul></ul></ul><ul><ul><ul><li>onOpen() </li></ul></ul></ul><ul><ul><ul><li>provide a database name, and a version (to super()) </li></ul></ul></ul><ul><ul><li>SQLiteStatement: precompiled SQL for fast reuse </li></ul></ul><ul><ul><ul><li>essentially, a JDBC PreparedStatement </li></ul></ul></ul>
  13. 13. SQLite Android SDK ships with sqlite3 tool <ul><ul><li>SQL console to SQLite files on device </li></ul></ul><ul><ul><li>generally you use adb to open a shell, then sqlite3: </li></ul></ul><ul><li>$ adb -s emulator-5554 shell </li></ul><ul><li># sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db </li></ul><ul><li>SQLite version 3.3.12 </li></ul><ul><li>Enter &quot;.help&quot; for instructions </li></ul><ul><li>.... enter commands, then quit... </li></ul><ul><li>sqlite> .exit </li></ul>
  14. 14. db4o <ul><li>db4o is a small-footprintobject database </li></ul><ul><ul><li>http://www.db4o.com </li></ul></ul><ul><ul><li>object database means no mapping files </li></ul></ul><ul><ul><ul><li>no ORM, no impedance mismatch, no external schema... </li></ul></ul></ul><ul><ul><li>simply define POJOs (no annotations required, even) </li></ul></ul>
  15. 15. db4o Opening the database <ul><li>ObjectContainer db = null; </li></ul><ul><li>try { </li></ul><ul><li>db = Db4o.openFile(&quot;mydata.db4o&quot;); </li></ul><ul><li>// . . . </li></ul><ul><li>} </li></ul><ul><li>catch (Exception ex) { </li></ul><ul><li>// Do more than just log the exception, please </li></ul><ul><li>} </li></ul><ul><li>finally { </li></ul><ul><li>if (db != null) </li></ul><ul><li>db.close(); </li></ul><ul><li>} </li></ul>
  16. 16. db4o Storing an object <ul><li>// Create the new Person </li></ul><ul><li>Person p = new Person(&quot;Matthew&quot;, &quot;Neward&quot;, 13, new Address(...)); </li></ul><ul><li>// Store </li></ul><ul><li>db.set(p); </li></ul><ul><li>db.commit(); </li></ul>
  17. 17. db4o Fetching an object (prototype-based) <ul><li>// Fetch all the Persons </li></ul><ul><li>ObjectSet persons = </li></ul><ul><li>// use empty/default object (all fields hold null/0 values) </li></ul><ul><li>db.get(new Person()); </li></ul><ul><li>// or could use type parameter: </li></ul><ul><li>// db.get(Person.class); </li></ul><ul><li>while (persons.hasNext()) </li></ul><ul><li>System.out.println(persons.next()); </li></ul><ul><li>// Fetch all the Newards </li></ul><ul><li>ObjectSet newards = db.get(new Person(null, &quot;Neward&quot;, 0, null); </li></ul><ul><li>while (newards.hasNext()) </li></ul><ul><li>System.out.println(newards.next()); </li></ul>
  18. 18. db4o Updating an object <ul><li>// Fetch Matthew Neward (note the absence of error-checking) </li></ul><ul><li>Person matthew = (Person) </li></ul><ul><li>db.get(new Person(&quot;Matthew&quot;, &quot;Neward&quot;, 0, null).next(); </li></ul><ul><li>// Happy Birthday! </li></ul><ul><li>matthew.setAge(matthew.getAge() + 1); </li></ul><ul><li>// Store </li></ul><ul><li>db.set(matthew); </li></ul><ul><li>db.commit(); </li></ul>
  19. 19. db4o <ul><li>db4o provides more functionality than just what's shown here </li></ul><ul><ul><li>Native queries </li></ul></ul><ul><ul><li>Automatic reference retrieval (&quot;fetch depth&quot;) </li></ul></ul><ul><ul><li>Native refactoring support </li></ul></ul><ul><ul><li>Store to memory or to disk (or across the network) </li></ul></ul><ul><ul><li>Transactions model </li></ul></ul><ul><ul><li>... and more </li></ul></ul>
  20. 20. Wrapping up <ul><li>&quot;What do you know?&quot; </li></ul>
  21. 21. Summary <ul><li>Android provides a local-storage option </li></ul><ul><ul><li>... that offers the traditional storage options </li></ul></ul><ul><ul><li>... that can be customized in many ways </li></ul></ul><ul><ul><li>... that ultimately will only get more powerful </li></ul></ul>
  22. 22. Resources <ul><li>Busy Coder's Guide to Android (and more) </li></ul><ul><ul><li>Mark Murphy, http://www.commonsware.com </li></ul></ul><ul><li>Android website </li></ul><ul><ul><li>http://developer.android.com </li></ul></ul><ul><li>Presentations by this guy </li></ul><ul><ul><li>Busy Android Dev's Guide to UI </li></ul></ul><ul><ul><li>Busy Android Dev's Guide to Communication </li></ul></ul><ul><ul><li>... and more </li></ul></ul>
  1. A particular slide catching your eye?

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

×