Mobile Application Development                         Lecture 17                    Vladimir Kulyukin             Departm...
Outline   ●       Programmatic SQLite Queries   ●       Java Threads   ●       Android Serviceshttp://www.youtube.com/vked...
Programmatic SQLite Querieshttp://www.youtube.com/vkedco   http://www.vkedco.blogspot.com
android.database.Cursor  ●      Cursor object is a result set of a query  ●      A result set can be thought of as a table...
Cursor Manipulation Logic           // 1. Obtain Cursor object          Cursor crsr = db.query( query arguments);         ...
BOOK_TITLE Table       Database Schema       ID | Title | BookAuthor | Translator | ISBN | Price      Data Rows      1|The...
BOOK_AUTHOR Table               Database Schema               ID | Name | BirthYear | DeathYear | Country               Da...
BOOK_COVER_IMAGE Table               Database Schema               ISBN | Image (BLOB)               Data Rows            ...
BOOK_ORDER Table               Database Schema               ID|ISBN|FirstName|LastName|Price                Data Rows    ...
SQLiteDatabase.query()           SQLiteDatabase db;           Cursor crsr =             db.query(String table,      // tab...
Example: sample_query_01()      static final String SQL_QUERY_01 = "SELECT * FROM book_title";      SQLiteDatabase db = th...
Example: sample_query_02()    static final String SQL_QUERY_02 = "SELECT Name, BirthYear FROM book_author";    static fina...
Example: sample_query_04()    static final String SQL_QUERY_04 =    "SELECT Title, BookAuthor, ISBN, Price FROM book_title...
Example: sample_query_05()    static final String SQL_QUERY_05 =    "SELECT Title, Translator, Price FROM book_title WHERE...
Example: sample_query_06()    static final String SQL_QUERY_06 =    "SELECT ISBN, SUM(Price) FROM book_order GROUP BY ISBN...
Example: sample_query_07()    static final String SQL_QUERY_07 =    "SELECT LastName, SUM(Price) FROM book_order GROUP BY ...
Java Threadshttp://www.youtube.com/vkedco   http://www.vkedco.blogspot.com
Definitions  ●      In Java, the term thread means two things:          – An instance of java.lang.Thread         –A   Thr...
Defining Threads  ●      A Thread can be defined in one of two ways:  ●      Extend java.lang.Thread and override the     ...
Extending java.lang.Thread        class MyThread extends Thread {              public void run() {                  System...
Implementing java.lang.Runnable        class MyRunnable implements Runnable {              public void run() {            ...
Using java.lang.Runnable               // 1. Define a job               MyRunnable job = new MyRunnable();               /...
Thread States                                   BLOCKED      NEW                                                        DE...
Using java.lang.Runnable      class MyNamedRunnable implements Runnable {                 public void run() {             ...
Android Serviceshttp://www.youtube.com/vkedco   http://www.vkedco.blogspot.com
Services  ●      Service is a component that runs in the      background  ●      Services support multitasking on Android ...
Services & Threads  ●      By default, services do not run in their own      threads  ●      By default, services run in t...
Service Declaration in AndroidManifest.xml        <service          android:name=”.ThreadedUpdateService”          android...
References   ●       www.sqlite.org/datatype3.html   ●       http://developer.android.com/reference/android/database/sqlit...
Feedback        Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco   http://www.vkedco.blogspot.com
Upcoming SlideShare
Loading in...5
×

Mobile Application Development: Lecture 17

618

Published on

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
618
On Slideshare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Mobile Application Development: Lecture 17

  1. 1. Mobile Application Development Lecture 17 Vladimir Kulyukin Department of Computer Science Utah State Universityhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● Programmatic SQLite Queries ● Java Threads ● Android Serviceshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  3. 3. Programmatic SQLite Querieshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  4. 4. android.database.Cursor ● Cursor object is a result set of a query ● A result set can be thought of as a table where each data row is a query response ● Number of elements in a result set can be found with Cursor.getCount() ● To move from data row to data row, one can use Cursor.moveToFirst() and Cursor.moveToNext()http://www.youtube.com/vkedco www.vkedco.blogspot.com
  5. 5. Cursor Manipulation Logic // 1. Obtain Cursor object Cursor crsr = db.query( query arguments); // 2. Check if result set has data rows if ( crsr.getCount() != 0 ) { // 3. Move to 1st data row crsr.moveToFirst(); while ( crsr.isAfterLast() == false ) { // 4. Do data row processing logic // 5. Move to next data row crsr.moveToNext(); } }http://www.youtube.com/vkedco www.vkedco.blogspot.com
  6. 6. BOOK_TITLE Table Database Schema ID | Title | BookAuthor | Translator | ISBN | Price Data Rows 1|The Essential Rumi|Jalal adDin Rumi|C. Barks, J. Moyne|9780062509581|17.51 2|The Illuminated Rumi|Jalal adDin Rumi|C. Barks|9780767900027|25.04 3|A Year with Rumi: Daily Readings|Jalal adDin Rumi|C. Barks|9780060845971|14.92 4|A Year with Hafiz: Daily Contemplations|Hafiz|D. Ladinsky|9780143117544|12.24 5|The Gift|Hafiz|Daniel Ladinsky|9780140195811|12.24http://www.youtube.com/vkedco www.vkedco.blogspot.com
  7. 7. BOOK_AUTHOR Table Database Schema ID | Name | BirthYear | DeathYear | Country Data Rows 1|Jalal adDin Rumi|1207|1273|Persia 2|Hafiz|1325|1389|Persiahttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  8. 8. BOOK_COVER_IMAGE Table Database Schema ISBN | Image (BLOB) Data Rows 9780062509581|BLOB_01 9780767900027|BLOB_02 9780060845971|BLOB_03 9780143117544|BLOB_04 9780140195811|BLOB_05http://www.youtube.com/vkedco www.vkedco.blogspot.com
  9. 9. BOOK_ORDER Table Database Schema ID|ISBN|FirstName|LastName|Price Data Rows 1|9780062509581|J.|A.|17.51 2|9780062509581|J.|A.|17.51 3|9780767900027|V.|K.|25.04 4|9780143117544|M.|N.|12.24 5|9780140195811|K.|R.|12.24 6|9780060845971|K.|R.|14.92 7|9780062509581|K.|R.|17.51http://www.youtube.com/vkedco www.vkedco.blogspot.com
  10. 10. SQLiteDatabase.query() SQLiteDatabase db; Cursor crsr = db.query(String table, // table name String[] columns, // array of column names String[] selectionArgs, // array of selection args String groupBy, // SQL GROUP BY clause String having, // SQL HAVING clause String orderBy // SQL ORDER BY clause );http://www.youtube.com/vkedco www.vkedco.blogspot.com
  11. 11. Example: sample_query_01() static final String SQL_QUERY_01 = "SELECT * FROM book_title"; SQLiteDatabase db = this.getReadableDatabase(); Cursor crsr = db.query(BOOK_TITLE_TABLE, // table name null, // column names null, // where clause null, // selection args null, // group by null, // having null);// order by // rest of code }http://www.youtube.com/vkedco www.vkedco.blogspot.com
  12. 12. Example: sample_query_02() static final String SQL_QUERY_02 = "SELECT Name, BirthYear FROM book_author"; static final String[] QUERY_02_COLNAMES = new String[]{AUTHOR_NAME_COL_NAME, AUTHOR_BIRTH_YEAR_COL_NAME}; SQLiteDatabase db = this.getReadableDatabase(); Cursor crsr = db.query(BOOK_AUTHOR_TABLE, // table name QUERY_02_COLNAMES, // column names null, // where clause null, // selection args null, // group by null, // having null); // order byhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  13. 13. Example: sample_query_04() static final String SQL_QUERY_04 = "SELECT Title, BookAuthor, ISBN, Price FROM book_title ORDER BY Price DESC"; static final String QUERY_04_ORDER_BY = BOOK_PRICE_COL_NAME + " DESC"; static final String[] QUERY_04_COLNAMES = new String[]{BOOK_TITLE_COL_NAME, BOOK_AUTHOR_COL_NAME, BOOK_PRICE_COL_NAME}; Cursor crsr = db.query(BOOK_TITLE_TABLE, // table name QUERY_04_COLNAMES, // column names null, // where clause null, // selection args null, // group by null, // having QUERY_04_ORDER_BY); // order byhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  14. 14. Example: sample_query_05() static final String SQL_QUERY_05 = "SELECT Title, Translator, Price FROM book_title WHERE Price < 15 or Translator=C. Barks ORDER BY Price DESC"; static final String[] QUERY_05_COLNAMES = new String[]{BOOK_TITLE_COL_NAME, BOOK_TRANSLATOR_COL_NAME, BOOK_PRICE_COL_NAME}; static final String QUERY_05_WHERE_CLAUSE = "Price < ? OR Translator = ?"; static final String[] QUERY_05_SELECTION_ARGS = new String[]{"15", "C. Barks"}; static final String QUERY_05_ORDER_BY = BOOK_PRICE_COL_NAME + " DESC"; SQLiteDatabase db = this.getReadableDatabase(); Cursor crsr = db.query(BOOK_TITLE_TABLE, // table name QUERY_05_COLNAMES, // column names QUERY_05_WHERE_CLAUSE, // where clause QUERY_05_SELECTION_ARGS, // selection args null, // group by null, // having BOOK_PRICE_COL_NAME + " DESC"); // order byhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  15. 15. Example: sample_query_06() static final String SQL_QUERY_06 = "SELECT ISBN, SUM(Price) FROM book_order GROUP BY ISBN ORDER BY Sum(Price) DESC;"; static final String[] QUERY_06_COLNAMES = new String[]{BOOK_ORDER_ISBN_COL_NAME, "SUM(Price)"}; static final String QUERY_06_GROUP_BY = BOOK_ORDER_ISBN_COL_NAME; static final String QUERY_06_ORDER_BY = "SUM(Price) DESC"; SQLiteDatabase db = this.getReadableDatabase(); Cursor crsr = db.query(BOOK_ORDER_TABLE, // table name QUERY_06_COLNAMES, // column names null, // where clause null, // selection args QUERY_06_GROUP_BY, // group by null, // having QUERY_06_ORDER_BY); // order byhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  16. 16. Example: sample_query_07() static final String SQL_QUERY_07 = "SELECT LastName, SUM(Price) FROM book_order GROUP BY LastName HAVING SUM(Price) > 20 ORDER BY SUM(Price) DESC;"; static final String[] QUERY_07_COLNAMES = new String[]{BOOK_ORDER_CLN_COL_NAME, "SUM(Price)"}; static final String QUERY_07_GROUP_BY = BOOK_ORDER_CLN_COL_NAME; static final String QUERY_07_HAVING = "SUM(Price) > 20"; static final String QUERY_07_ORDER_BY = "SUM(Price) DESC"; SQLiteDatabase db = this.getReadableDatabase(); Cursor crsr = db.query(BOOK_ORDER_TABLE, // table name QUERY_07_COLNAMES, // column names null, // where clause null, // selection args QUERY_07_GROUP_BY, // group by QUERY_07_HAVING, // having QUERY_07_ORDER_BY); // order byhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  17. 17. Java Threadshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  18. 18. Definitions ● In Java, the term thread means two things: – An instance of java.lang.Thread –A Thread of execution ● An instance of java.lang.Thread is an object ● A Thread of execution is a lightweight process with its own stackhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  19. 19. Defining Threads ● A Thread can be defined in one of two ways: ● Extend java.lang.Thread and override the run() method ● Implement a class that implements java.lang.Runnable ● From the OOP perspective, implementing java.lang.Runnable is better than extending java.lang.Threadhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  20. 20. Extending java.lang.Thread class MyThread extends Thread { public void run() { System.out.println(“this is MyThread.run()”); } }http://www.youtube.com/vkedco www.vkedco.blogspot.com
  21. 21. Implementing java.lang.Runnable class MyRunnable implements Runnable { public void run() { System.out.println(“this is MyRunnable.run()”); } }http://www.youtube.com/vkedco www.vkedco.blogspot.com
  22. 22. Using java.lang.Runnable // 1. Define a job MyRunnable job = new MyRunnable(); // 2. Mount the job on multiple worker threads Thread worker01 = new Thread(job); Thread worker02 = new Thread(job); Thread worker03 = new Thread(job);http://www.youtube.com/vkedco www.vkedco.blogspot.com
  23. 23. Thread States BLOCKED NEW DEAD Runnable Runninghttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  24. 24. Using java.lang.Runnable class MyNamedRunnable implements Runnable { public void run() { for(int j = 0; j <= 400; j++ { System.out.println(“Thread “ + Thread.currentThread().getName() + “j = “ + j); } } } MyNamedRunnable job = new MyNamedRunnable(); Thread worker01 = new Thread(job); worker01.setName(“Worker 01”); Thread worker02 = new Thread(job); worker02.setName(“Worker 02”); Thread worker03 = new Thread(job); worker03.setName(“Worker 03”); worker01.start(); worker02.start(); worker03.start();http://www.youtube.com/vkedco www.vkedco.blogspot.com
  25. 25. Android Serviceshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  26. 26. Services ● Service is a component that runs in the background ● Services support multitasking on Android because they can be made to run in their own processes ● Services are more flexible than threads because, unlike services, threads are tied to activities that start themhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  27. 27. Services & Threads ● By default, services do not run in their own threads ● By default, services run in the main thread of the hosting process ● If a service must run computation-intensive tasks, it should spawn its own threadshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  28. 28. Service Declaration in AndroidManifest.xml <service android:name=”.ThreadedUpdateService” android:process=”:org.vkedco.android.updateservice” android:icon=”@drawable/my_service_icon_01” android:label=”@string/my_update_service_label” > </service> ● The android:process means that this service runs in its own process ● The colon in the android:process name is that the service is private to its application ● The absence of colon is that the service is a global processhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  29. 29. References ● www.sqlite.org/datatype3.html ● http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html ● http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html ● http://developer.android.com/reference/android/app/Service.htmlhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  30. 30. Feedback Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com

×