Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Similar documents
CS371m - Mobile Computing. Responsiveness

Produced by. Design Patterns. MSc Computer Science. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

32. And this is an example on how to retrieve the messages received through NFC.

Produced by. Mobile Application Development. Eamonn de Leastar

Mobile Application Development Android

Communicating with a Server

Mobile Application Development Android

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Mobile Application Development Android

Hydrogen Car Mobile Display

Overview of Frameworks: Part 3

Produced by. App Development & Modelling. Eamonn de Leastar

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Mobile Computing. Introduction to Android

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Android Connectivity & Google APIs

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

CMSC434. Introduction to Human-Computer Interaction. Week 11 Lecture 18 April 5, 2016 Engineering Interfaces IV. Jon

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables.

Mobile and Ubiquitous Computing: Android Programming (part 4)

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Produced by. Agile Software Development. Eamonn de Leastar

CHAPTER 15 ASYNCHRONOUS TASKS

Produced by. Mobile Application Development. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Android" Application Development SAMS. Sams Teach Yourself. Shane Conder. Lauren Darcey. Second Edition

Produced by. App Development & Modelling. BSc in Applied Computing. Eamonn de Leastar

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Design Patterns. Produced by. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Mobile Programming Practice Background processing AsynTask Service Broadcast receiver Lab #5

Object Oriented Concepts. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Efficient Android Threading

The Myx Architectural Style

IEMS 5722 Mobile Network Programming and Distributed Server Architecture

Asynchronous Database Access with Qt 4.x

application components

Answers to Exercises

Produced by. Mobile Application Development. Eamonn de Leastar

ANDROID DEVELOPMENT. Course Details

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Open Closed Principle (OCP)

CSE 331 Software Design & Implementation

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Software Practice 3 Today s lecture Today s Task

Android Essentials with Java

CS 351 Design of Large Programs Singleton Pattern

CS 160: Interactive Programming

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

GUI in C++ PV264 Advanced Programming in C++ Nikola Beneš Jan Mrázek Vladimír Štill. Faculty of Informatics, Masaryk University.

Topics Covered in the Android Apps Development Training

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Produced by. Web Development. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Android Services & Local IPC: The Command Processor Pattern (Part 1)

Race Detection for Android Applications

Produced by. Design Patterns. MSc Computer Science. Eamonn de Leastar

Document Plan. Social Media Geolocation App. Robert McCaffrey, Matt Feld, Taylor Neal, David Kwon BLIPME

Java Concurrency in practice Chapter 9 GUI Applications

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Scippa: System-Centric IPC Provenance on Android

Algorithms. Produced by. Eamonn de Leastar

Minds-on: Android. Session 2

BSc Applied Computing / Forensics / Entertainment Systems/ IOT. Eamonn de Leastar Dr. Brenda Mullally

Services. service: A background task used by an app.

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

COMP4521 EMBEDDED SYSTEMS SOFTWARE

Fragments and the Maps API

Software Engineering Large Practical: Accessing remote data and XML parsing. Stephen Gilmore School of Informatics October 8, 2017

Produced by. Agile Software Development. Eamonn de Leastar

MRCP. Client Integration Manual. Developer Guide. Powered by Universal Speech Solutions LLC

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

Produced by. App Development & Modeling. BSc in Applied Computing. Eamonn de Leastar

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

EPITA Première Année Cycle Ingénieur. Atelier Java - J5

User Interfaces for Web Sites and Mobile Devices. System and Networks

Lecture 2 Android SDK

Produced by. Web Development. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Single Responsibility Principle (SRP)

Chapter 0 : MVC review / Threading & Concurrency. CSCI 251 Android App Development

Programming in Android. Nick Bopp

The Singleton Pattern. Design Patterns In Java Bob Tarr

RUNTIME PERMISSIONS IN ANDROID 6.0 Lecture 10a

CS378 -Mobile Computing. Services and Broadcast Receivers

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Asynchronous Android

CS378 - Mobile Computing. Anatomy of an Android App and the App Lifecycle

CS 4300 Computer Graphics

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Produced by. App Development & Modeling. BSc in Applied Computing. Eamonn de Leastar

MOBILE COMPUTING 1/20/18. How many of you. CSE 40814/60814 Spring have implemented a command-line user interface?

Windows and Events. created originally by Brian Bailey

Transcription:

Design Patterns MSc in Computer Science Produced by Eamonn de Leastar (edeleastar@wit.ie)! Department of Computing, Maths & Physics Waterford Institute of Technology http://www.wit.ie http://elearning.wit.ie

Android Threads

Threads in Android When an application is launched, the system creates a thread of execution for the application, called "main." This thread is in charge of dispatching events to the appropriate user interface widgets, including drawing events. It is also the thread in which your application interacts with components from the Android UI toolkit (components from the android.widget and android.view packages). As such, the main thread is also sometimes called the UI thread.

Threads and UI Components The system does not create a separate thread for each instance of a component. All components that run in the same process are instantiated in the UI thread, and system calls to each component are dispatched from that thread. Consequently, methods that respond to system callbacks (such as onkeydown() to report user actions or a lifecycle callback method) always run in the UI thread of the process.

ANR Dialog When your app performs intensive work in response to user interaction, this single thread model can yield poor performance unless you implement your application properly. Specifically, if everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI. When the thread is blocked, no events can be dispatched, including drawing events. From the user's perspective, the application appears to hang. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog.

Worker Threads To retain application responsiveness do not block the UI thread. If you have operations to perform that are not instantaneous, you should make sure to do them in separate threads ("background" or "worker" threads).

At first, this seems to work fine, because it creates a new thread to handle the network operation. However, it violates the second rule of the singlethreaded model: do not access the Android UI toolkit from outside the UI thread this sample modifies the ImageView from the worker thread instead of the UI thread. This can result in undefined and unexpected behavior, which can be difficult and time-consuming to track down.

Android Solution To fix this problem, Android offers several ways to access the UI thread from other threads. Here is a list of methods that can help: Activity.runOnUiThread(Runnable) View.post(Runnable) View.postDelayed(Runnable, long)

View.postDelayed(Runnable, long) Now this implementation is thread-safe: the network operation is done from a separate thread while the ImageView is manipulated from the UI thread. However, as the complexity of the operation grows, this kind of code can get complicated and difficult to maintain. To handle more complex interactions with a worker thread, you might consider using a Handler in your worker thread, to process messages delivered from the UI thread.

Async Tasks AsyncTask allows you to perform asynchronous work on your user interface. It performs the blocking operations in a worker thread and then publishes the results on the UI thread, without requiring you to handle threads and/or handlers yourself. To use it, you must subclass AsyncTask and implement the doinbackground() callback method, which runs in a pool of background threads. To update your UI, you should implement onpostexecute(), which delivers the result from doinbackground() and runs in the UI thread, so you can safely update your UI. You can then run the task by calling execute() from the UI thread.

Now the UI is safe and the code is simpler, because it separates the work into the part that should be done on a worker thread and the part that should be done on the UI thread.

Async Tasks You should read the AsyncTask reference for a full understanding on how to use this class, but here is a quick overview of how it works: You can specify the type of the parameters, the progress values, and the final value of the task, using generics The method doinbackground() executes automatically on a worker thread onpreexecute(), onpostexecute(), and onprogressupdate() are all invoked on the UI thread The value returned by doinbackground() is sent to onpostexecute() You can call publishprogress() at anytime in doinbackground() to execute onprogressupdate() on the UI thread You can cancel the task at any time, from any thread

Except where otherwise noted, this content is licensed under a Creative Commons Attribution-NonCommercial 3.0 License.! For more information, please see http:// creativecommons.org/licenses/by-nc/3.0/