• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Mobile Application Development: Lecture 17
 

Mobile Application Development: Lecture 17

on

  • 804 views

 

Statistics

Views

Total Views
804
Views on SlideShare
415
Embed Views
389

Actions

Likes
0
Downloads
0
Comments
0

22 Embeds 389

http://vkedco.blogspot.com 171
http://www.vkedco.blogspot.com 107
http://vkedco.blogspot.co.uk 21
http://vkedco.blogspot.in 19
http://vkedco.blogspot.com.br 17
http://vkedco.blogspot.com.es 11
http://vkedco.blogspot.co.il 8
http://vkedco.blogspot.mx 7
http://vkedco.blogspot.fi 6
http://vkedco.blogspot.ca 4
http://www.vkedco.blogspot.in 4
http://vkedco.blogspot.fr 3
http://vkedco.blogspot.de 2
http://vkedco.blogspot.sk 1
http://vkedco.blogspot.pt 1
http://vkedco.blogspot.nl 1
http://vkedco.blogspot.ch 1
http://vkedco.blogspot.cz 1
http://www.vkedco.blogspot.co.uk 1
http://www.blogger.com 1
http://vkedco.blogspot.hu 1
http://vkedco.blogspot.sg 1
More...

Accessibility

Categories

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.

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

    Mobile Application Development: Lecture 17 Mobile Application Development: Lecture 17 Presentation Transcript

    • Mobile Application Development Lecture 17 Vladimir Kulyukin Department of Computer Science Utah State Universityhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Outline ● Programmatic SQLite Queries ● Java Threads ● Android Serviceshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • 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 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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 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
    • 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
    • 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
    • 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
    • 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
    • Thread States BLOCKED NEW DEAD Runnable Runninghttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • 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
    • 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 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
    • 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
    • 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
    • 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
    • Feedback Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com