Android Developer Fundamentals V2

Slides:



Advertisements
Similar presentations
CE881: Mobile and Social Application Programming Simon M. Lucas Layouts.
Advertisements

USING ANDROID WITH THE INTERNET. Slide 2 Network Prerequisites The following must be included so as to allow the device to connect to the network The.
Lec 06 AsyncTask Local Services IntentService Broadcast Receivers.
Threads, AsyncTasks & Handlers.  Android implements Java threads & concurrency classes  Conceptual view  Parallel computations running in a process.
Intro to Threading CS221 – 4/20/09. What we’ll cover today Finish the DOTS program Introduction to threads and multi-threading.
Cosc 4730 Brief return Sockets And HttpClient And AsyncTask.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Android: Event Handler Blocking, Android Inter-Thread, Process Communications 10/11/2012 Y. Richard Yang.
HTTP and Threads. Download some code I’ve created an Android Project which gives examples of everything covered in this lecture. Download code here.here.
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
UI Design Patterns & Best Practices Mike Wolfson July 22, 2010.
CS378 - Mobile Computing Web - WebView and Web Services.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
Software Architecture of Android Yaodong Bi, Ph.D. Department of Computing Sciences University of Scranton.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
Cosc 4730 Brief return Sockets And HttpClient (and with AsyncTask) DownloadManager.
Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
1 Mobile Software Development Framework: Android 2/28/2011 Y. Richard Yang.
Networking Android Club Networking AsyncTask HttpUrlConnection JSON Parser.
CS378 - Mobile Computing Web - WebView and Web Services.
HTTP and Threads. Download some code I’ve created an Android Project which gives examples of everything covered in this lecture. Download code here.here.
HTTP and Threads. Getting Data from the Web Believe it or not, Android apps are able to pull data from the web. Developers can download bitmaps and text.
Address Book App 1. Define styles   Specify a background for a TextView – res/drawable/textview_border.xml.
Introduction to Socket Programming in Android Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Concurrent Programming and Threads Threads Blocking a User Interface.
CS378 - Mobile Computing Responsiveness. An App Idea From Nifty Assignments Draw a picture use randomness Pick an equation at random Operators in the.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ALAA M. ALSALEHI SOFTWARE ENGINEER AT IUG Multithreading in Android.
Android Threads. Threads Android will show an “ANR” error if a View does not return from handling an event within 5 seconds Or, if some code running in.
Recap of Part 1 Terminology Windows FormsAndroidMVP FormActivityView? ControlViewView? ?ServiceModel? Activities Views / ViewGroups Intents Services.
Address Book App 1 Fall 2014 CS7020: Game Design and Development.
Lecture 6: Process and Threads Topics: Process, Threads, Worker Thread, Async Task Date: Mar 1, 2016.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
HUJI Post PC Workshop 5 Files, Threading, and Web Services Sasha Goldshtein
USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Developing Android Services. Objectives Creating a service that runs in background Performing long-running tasks in a separate thread Performing repeated.
Threads and Swing Multithreading. Contents I. Simulation on Inserting and Removing Items in a Combo Box II. Event Dispatch Thread III. Rules for Running.
Multithreading Chapter 6. Objectives Understand the benefits of multithreading on Android Understand multi-threading fundamentals Know the Thread class.
Concurrency in Android
Small talk with the UI thread
UI Design Patterns & Best Practices
Content Providers And Content Resolvers
Asynchronous Task (AsyncTask) in Android
Reactive Android Development
Instructor: Mazhar Hussain
ListView: Part 2.
Concurrency in Android
CS240: Advanced Programming Concepts
Lecture 6: Process, Thread, Task
CS499 – Mobile Application Development
Developing Android Services
Android Programming Lecture 8
Mobile Software Development Framework: Android
CIS 470 Mobile App Development
CS371m - Mobile Computing Responsiveness.
Android Topics UI Thread and Limited processing resources
Mobile Computing With Android ACST 4550 Android Database Storage
Android Topics Asynchronous Callsbacks
Activities and Intents
9. Threads SE2811 Software Component Design
Threads, Handlers, and AsyncTasks
Threads in Java James Brucker.
CIS 470 Mobile App Development
Using threads for long running tasks.
Lecture 6: Process, Thread, Task
Android Threads Dimitar Ivanov Mobile Applications with Android
Presentation transcript:

Android Developer Fundamentals V2 Background Tasks

Threads

The main thread Independent path of execution in a running program Code is executed line by line App runs on Java thread called "main" or "UI thread" Draws UI on the screen Responds to user actions by handling UI events

The Main thread must be fast Hardware updates screen every 16 ms UI thread has 16 ms to do all its work If it takes too long, app stutters or hangs If the UI waits too long for an operation to finish, it becomes unresponsive The framework shows an Application Not Responding (ANR) dialog

What is a long running task? What is a long running task? Network operations Long calculations Downloading/uploading files Processing images Loading data

Two rules for Android threads Do not block the UI thread Complete all work in less than 16 ms for each screen Run slow non-UI work on a non-UI thread Do not access the Android UI toolkit from outside the UI thread Do UI work only on the UI thread

Background threads Execute long running tasks on a background (worker) thread A worker thread is any thread which is not the main or UI thread. AsyncTask The Loader Framework Services Main Thread (UI Thread) Update UI Worker Thread Do some work The method depends on the use case

AsyncTask

What is AsyncTask? Use AsyncTask to implement basic background tasks

Override two methods doInBackground()—runs on a background thread All the work to happen in the background onPostExecute()—runs on main thread when work done Process results Publish results to the UI

AsyncTask helper methods onPreExecute() Runs on the main thread Sets up the task onProgressUpdate() receives calls from publishProgress() from background thread

Creating an AsyncTask Subclass AsyncTask Provide data type sent to doInBackground() Provide data type of progress units for onProgressUpdate() Provide data type of result for onPostExecute()

MyAsyncTask class definition private class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {...} doInBackground() doInBackground( String—could be query, URI for filename Integer—percentage completed, steps done Bitmap—an image to be displayed Use Void if no data passed doInBackground() onProgressUpdate() onPostExecute()

doInBackground() protected void onPreExecute() { // display a progress bar } protected Bitmap doInBackground(String... query) { // Get the bitmap return bitmap;

doInBackground() protected void onProgressUpdate(Integer... progress) { setProgressPercent(progress[0]); } protected void onPostExecute(Bitmap result) { // Do something with the bitmap

Start background work String query = mEditText.getText().toString(); public void loadImage (View view) { String query = mEditText.getText().toString(); new MyAsyncTask(query).execute(); }

Start background work public class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { protected Long doInBackground(URL... urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); if (isCancelled()) break; } return totalSize; protected void onProgressUpdate(Integer... progress) { setProgressPercent(progress[0]); } protected void onPostExecute(Long result) { showDialog("Downloaded " + result + " bytes"); } new DownloadFilesTask().execute(url1, url2, url3);

Cancelling an AsyncTask We can cancel a task at any time by invoking the cancel() method. The cancel() method returns false if the task could not be cancelled, typically because it has already completed. To find out whether a task has been cancelled, check the return value of isCancelled() periodically from doInBackground. After an AsyncTask task is cancelled, onPostExecute() will not be invoked after doInBackground() returns. Instead, onCancelled(Object) is invoked.

Limitations of AsyncTask Limitations of AsyncTask When device configuration changes, Activity is destroyed AsyncTask cannot connect to Activity anymore New AsyncTask created for every config change Old AsyncTasks stay around App may run out of memory or crash

Loaders

What is a Loader? Provides asynchronous loading of data Reconnects to Activity after configuration change Can monitor changes in data source and deliver new data Callbacks implemented in Activity Many types of loaders available AsyncTaskLoader, CursorLoader

Anatomy of a Loader What is a LoaderManager? Manages loader functions via callbacks Can manage multiple loaders loader for database data, for AsyncTask data, for internet data…

Get a loader with initLoader() Creates and starts a loader, or reuses an existing one, including its data Use restartLoader() to clear data in existing loader getLoaderManager().initLoader(Id, args, callback); getLoaderManager().initLoader(0, null, this); getSupportLoaderManager().initLoader(0, null, this);

Implementing AsyncTaskLoader

AsyncTaskLoader Overview WorkToDo LoaderManager Request Work Receive Result Activity

AsyncTask AsyncTaskLoader doInBackground() onPostExecute() loadInBackground() onLoadFinished()

Steps for AsyncTaskLoader subclass Subclass AsyncTaskLoader Implement constructor loadInBackground() onStartLoading()

Subclass AsyncTaskLoader public static class StringListLoader extends AsyncTaskLoader<List<String>> { public StringListLoader(Context context, String queryString) { super(context); mQueryString = queryString; }

loadInBackground() public List<String> loadInBackground() { List<String> data = new ArrayList<String>(); //TODO: Load the data from the network or from a database return data; }

onStartLoading() When restartLoader() or initLoader() is called, the LoaderManager invokes the onStartLoading() callback Check for cached data Start observing the data source (if needed) Call forceLoad() to load the data if there are changes or no cached data protected void onStartLoading() { forceLoad(); }

Implement loader callbacks in Activity onCreateLoader() — Create and return a new Loader for the given ID onLoadFinished() — Called when a previously created loader has finished its load onLoaderReset() — Called when a previously created loader is being reset making its data unavailable

onCreateLoader() @Override public Loader<List<String>> onCreateLoader(int id, Bundle args) { return new StringListLoader(this,args.getString("queryString")); }

onLoadFinished() Results of loadInBackground() are passed to onLoadFinished() where you can display them public void onLoadFinished(Loader<List<String>> loader, List<String> data) { mAdapter.setData(data); }

onLoaderReset() Only called when loader is destroyed Leave blank most of the time @Override public void onLoaderReset(final Loader<List<String>> loader) { }

Get a loader with initLoader() In Activity Use support library to be compatible with more devices getSupportLoaderManager().initLoader(0, null, this);

END