DOC PREVIEW
SDSU CS 696 - Android Threads

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 696 Emerging Web and Mobile TechnologiesSpring Semester, 2011Doc 23 Android ThreadsApr 19, 2011Copyright ©, All rights reserved. 2011 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/opl.shtml) license defines the copyright on this document.Wednesday, April 20, 2011References2The Busy Coder's Guide to Android Development, V2.1, Mark L. MurphyWednesday, April 20, 20113Processes and ThreadsProcesses have own address spaceTake longer to startConsume more memoryThreads share address spaceWednesday, April 20, 2011Android, Processes and Threads4Android application starts withOne process running one threadThe thread is called the main or UI threadActivity code runs in main (UI) threadCan create more threads to run in same processCan configure activities to run in separate processesNot as common as creating threadsWednesday, April 20, 2011Android Thread Rules5Don't block the UI threadActivity code runs on the UI threadCreate threads to perform long operationsDo not access the Android UI toolkit from outside the UI threadUse the following to access UI thread Activity.runOnUiThread(Runnable)View.post(Runnable)View.postDelayed(Runnable, long)Wednesday, April 20, 20116Android Background ToolsJava threadsHandlerMessagesRunnablesAsyncTaskServicesWednesday, April 20, 2011Bad Java Thread Example7public void onClick(View v) { new Thread(new Runnable() { public void run() { Bitmap b = loadImageFromNetwork("http://example.com/image.png"); mImageView.setImageBitmap(b); } }).start();}Don't do this Wednesday, April 20, 2011Done Correctly8public void onClick(View v) { new Thread(new Runnable() { public void run() { final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png"); mImageView.post(new Runnable() { public void run() { mImageView.setImageBitmap(bitmap); } }); } }).start();}Wednesday, April 20, 20119AsyncTaskWednesday, April 20, 2011AsyncTask10Replaces threads & MessagesAndroid 1.5Subclass AsyncTaskonPreExecute()Run in UI threadDone firstdoInBackground(Params...)Run in seperate threadpublishProgress(Progress...)Call in doInBackground() to register progressonProgressUpdate(Progress...)Run in UI threadCalled by publishProgressonPostExecute(Result)Run in UI threadRun after doInBackground endsWednesday, April 20, 2011Rules11The AsyncTask subclass instance must be created on the UI threadexecute(Params...) Starts the taskMust be invoked on the UI threadDo not call manuallyonPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...)The task can be executed only once Wednesday, April 20, 2011AsyncTask Types12private class SampleTask extends AsyncTask<Params, Progress, Result>ParamsType of argument for doInBackground()execute()ProgressType of argument for publishProgress()onProgressUpdate()ResultReturn type for doInBackground()Type of argument for onPostExecute()Wednesday, April 20, 2011How it Works13UI Thread Background Threadprivate class SampleTask extends AsyncTask<Params, Progress, Result>new SampleTask().execute(paramsType);onPreExecute()doInBackground(Params... stuff){blahpublishProgress(progressType);blahreturn x;}onProgressUpdate(Progress... values)onPostExecute(Result result)Wednesday, April 20, 2011Example14Loops in the background and displays Toast Wednesday, April 20, 2011ThreadExample15public class ThreadExample extends Activity { private class SampleTask extends AsyncTask<String, String, Void> { protected Void doInBackground(String... words) { for (String word : words) { publishProgress(word); SystemClock.sleep(1000); } return (null); } protected void onPostExecute(Void unused) { Toast.makeText(ThreadExample.this, "Done", Toast.LENGTH_SHORT) .show(); }Wednesday, April 20, 2011ThreadExample16 protected void onPreExecute() { Toast.makeText(ThreadExample.this, "Start", Toast.LENGTH_SHORT).show(); } protected void onProgressUpdate(String... word) { Toast.makeText(ThreadExample.this, word[0], Toast.LENGTH_SHORT).show(); } } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onStart() { super.onStart(); String[] text = { "Bat", "cat", "dat", "fat", "hat", "mat" }; new SampleTask().execute(text); }}Wednesday, April 20, 2011Just For Fun17Dynamically updateList ViewWednesday, April 20, 2011The Task18public class ThreadExample extends ListActivity { private class SampleTask extends AsyncTask<Void, String, Void> { String[] items = { "Gautama Buddha", "Kalki", "Krishna", "Kurma", "Matsya", "Narasimha", "Parashurama", "Rama", "Vamana", "Varaha" }; protected Void doInBackground(Void... notused) { for (String word : items) { publishProgress(word); SystemClock.sleep(2500); } return (null); } protected void onPostExecute(Void unused) { Toast.makeText(ThreadExample.this, "Done", Toast.LENGTH_SHORT) .show(); } protected void onProgressUpdate(String... word) { listAdapter.add(word[0]); } }Wednesday, April 20, 2011Based on an example from The Busy Coder's Guide to Android Development, V2.1, Mark L. MurphyThe Activity19 private ArrayAdapter<String> listAdapter; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>()); setListAdapter(listAdapter); new SampleTask().execute(); }}Wednesday, April 20, 201120HandlerWednesday, April 20, 2011Handler21Attached to thread it is created inProcesses Message objectsRunnable objectsMain UsesSchedule messages/runnables to be executed as in the futureEnqueue an action to be performed on a different threadWednesday, April 20, 2011Handler Scheduling22post(Runnable)postAtTime(Runnable, long)postDelayed(Runnable, long)sendEmptyMessage(int)sendMessage(Message)sendMessageAtTime(Message, long)sendMessageDelayed(Message, long)Wednesday, April 20, 2011ProgressBar Example23Just shows a progress bar progressingWednesday, April 20, 2011Example from The Busy Coder's Guide to Android Development, V2.1, Mark L. MurphyThreadExample24public class ThreadExample extends Activity { ProgressBar progressView; boolean


View Full Document

SDSU CS 696 - Android Threads

Download Android Threads
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Android Threads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Android Threads 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?