-
Threads and the Async Task Class
The User Interface (UI) Thread
Programs developed in Java with a Graphical interface have a specific thread which handles the interaction between the user and the graphical interface. This imposes certain restrictions on the use of developer defined threads which need to interact with the graphical interface.
The UI Thread in Android
In Android the UI Thread performs modifications to the User Interface. If you perform a long lasting operation to performing some calculations this may cause the UI Code to “block” until that operation is finished. The application may then appear to “freeze” which is not desirable.
Android will also display an “Application not responding” dialogue if a running activity does not respond within 5 seconds. The user is given the option to close the app at that point.
UI Threads and Background Threads
- There are therefore a number of occasions where it is necessary to carry out some processing in a background thread. Typical examples are:
- Network access (Accessing a web page for example)
- Database Access
- Performing a calculation to display an object
Support for Threads
So what support is available for background Threads?
-
Asynchronous (Async) Task
This class allows background tasks to perform operations and publish results on the UI thread without having to manipulate threads and/or handlers. This class is useful when you need to implement short lived tasks such as accessing/downloading a web resource.
- An asynchronous task is created with 3 generic types:
- called Params,
- Progress
- Result
- Four methods are implemented using:
- onPreExecute,
- doInBackground
- onProgressUpdate
- onPostExecute
AsyncTask's Generic Types
- The three types used by an asynchronous task are the following:
- Params, the type of the parameters sent to the task upon execution.
- Progress, the type of the progress units published during the background computation.
- Result, the type of the result of the background computation.
It is worth to note that not all types are always used by an asynchronous task and to mark a type as unused use the type Void: for example:
private class MyTask extends AsyncTask<Void, Void, Void> { ... }
-
Async Task Execution
- When an asynchronous task is executed the 4 methods are executed in a specific order.
- onPreExecute()
- doInBackground(Params …)
- onProgressUpDate(Progress …)
- onPostExecute(Result)
onPreExecute() method is invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface
doInBackground(Params...) is invoked on the background thread immediately after onPreExecute() finishes executing.
This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
onProgressUpdate(Progress...) is invoked on the UI thread after a call to publishProgress(Progress...).
This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
onPostExecutute(Result) is invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
-
Async Task
The Async class must be subclassed to be used. You will normally override the method (doInBackground(Params...)) and may also override (onPostExecute(Result).)
Example
public void onClick(View v) { // If download button pressed // starting new Async Task new DownloadfromTVURL().execute(fileURL); } class DownloadfromfromTVURL extends AsyncTask<String, String, String> { // Methods go here } protected String doInBackground(String... f_url) { InputStream anInStream; try { anInStream = OpenHttpConnection(f_url[0]); } //etc. }
-
Human Computer Interaction Issues in Android
From this section onward, the presentation slides are simply intended to provide some direction to the contents of the HCI paper. See the handout “Guidelines for handheld Mobile Device Interface Design” by Gong and Tarasewich. The HCI Paper will provide you with some pointers on what you should be doing with the Graphical interface for your coursework.
Shneiderman’s Golden Rules
Much of the work in the area of Interface Design is attributed to B. Shneiderman “Designing the user interface – Strategies for Effective Human-Computer Interaction”. We are interested in the application of these golden rules to mobile interface design to enable Frequent users to use shortcuts and reducing the number of operations required to carry out regular tasks.
-
Shneiderman’s Golden Rules
- Offer informative feedback
- Feedback should be substantial and informative to the user
- Every operator action should result in feedback
- Design Dialogs to yield Closure
- Organize actions into groups
- Give users satisfaction of accomplishment and completion
- Support internal locus of control
- Users should feel in control
- Design to allow users to initiate actions
- Consistency
- Across multiple platforms
- Look and feel should be the same
- Create input/output methodologies that are device independent
- Reversal of Actions
- Reversal of actions (Think undo operation)
- Memory can be an issue to store states
- Error prevention and simple error handling
- Can be problematic due to size of “keyboard”
- Focus on appropriate input format (email, numbers etc.)
- Reduce short term memory load
-
Additional Guidelines for Mobile Device Design
- Design for Multiple and Dynamic Contexts
- Allow output to be configured to users’ needs and preferences
- Allow for single and no handed operation
- Additional Guidelines for Mobile Device Design
- Design for Small Devices
- Provide Word selection
- Predictive Text
- Design for limited and split attention
- Provide sound output options
- Provide tactile output options
- Design for speed and recovery
- App can stop, start and resume with little effort
- App should be up and running quickly
- Design for Top Down Interaction
- Provide high levels of information and let users decide if they want more
- Allow for personalisation
- Allow for personalization though settings, setup etc.
- Design for Enjoyment
- App should be visually pleasing and fun as well as usable