Android Topics Asynchronous Callsbacks 1 Synchronous vs Asynchronous
Android Topics Asynchronous Callsbacks 1. Synchronous vs Asynchronous 2. Overview Async. Task 3. Explore Async. Task documentation 4. Basic Understanding of Async. Task callbacks 5. Async. Task parameters 6. Explore a Sample Structure of a simple Async. Task Class 7. Another Examlpe 8. Rules for using Async. Task 9. Practice 10. Review
Topic 1: Synchronous vs. Asynchronous • Most of the code that you've written has been synchronous. • Synchronous means that each line of code gets executed one right after another. • Synchronous code requires us to wait for each line to finish executing before going on to the next line. • Asynchronous uses callbacks to remove this sequence.
Quick look at Synchronous vs Asynchronous Synchronous Waiting for something to happen (and doing nothing else until it happens!) before proceeding. Asynchronous Do something else while you’re waiting for the event to happen. When the event happens, you will get called back, and you can jump into action.
Basics of Async Callbacks in Android • Asynchronous means that we tell the system we're interested in a certain event, like a click event. • The system will notify us, or call us back when that event happens. • In the meantime, we can go do something else in the app. • This means we're not sitting around stalled or waiting. We can still do other things like scroll a list, or wait for other user input events. • When the event happens, or if it even happens at all, the system will call us back and we can execute instructions that we specified.
Asynchronous callbacks is a common pattern in mobile apps.
Topic 2: Over. View of Async. Task Use an Android Async. Task to execute some long-running code that needs to update the UI. Useful for the following: a. Run a specified long-running task on another thread b. Once this thread completes the specified task, it will perform updates to the UI. An asynchronous task (Async. Task) is defined by a computation that runs on a background thread and whose result is published on the UI thread.
Topic 3: Explore Async. Task documentation • Explore how to set up listeners for events. • Learn how to hook into these events and be able to respond to them. • http: //developer. android. com/reference/android/os/Async. Task. html What did you discover?
Basic Understanding of Async. Task callbacks An asynchronous task is defined by 4 steps: • on. Pre. Execute, • do. In. Background, • on. Progress. Update and • on. Post. Execute. •
on. Pre. Execute() • Invoked on the UI thread before the task is executed. • This step is normally used to setup the task.
do. In. Background(Params. . . ) • invoked to perform your long-running work. • The input parameters from execute are passed into this method • invoked on the background thread immediately after on. Pre. Execute() has finished executing.
on. Progress. Update(Progress. . . ) • invoked on the UI thread after a call to publish. Progress(Progress. . . ). • This method is used to display any form of progress in the user interface while the background computation is still executing
on. Post. Execute(Result) • This method is used to update the UI once the background work is completed. • This callback is invoked on the UI thread. • The result of the background computation is passed to this step as a parameter.
Async. Task Parameters Async. Task utilizes three parameters: • android. os. Async. Task<Params, Progress, Result> • These parameters can be of any type.
Async. Task Parameters The three types used by an asynchronous task are the following: • Params — the type of the parameters sent to the task upon execution the type of parameters passed into execute(). • Progress — the type of the progress units published during the background computation. • Result — the type of the result of the background computation must be the return type of do. In. Background(). Params Progress Result Example: private class My. Async. Task extends Async. Task<String, Void, int >
Async. Task Parameters • Not all parameters need to used by an asynchronous task. • To mark a type as unused, use the type Void: Example: private class My. Task extends Async. Task<Void, Void> {. . . }
Explore the Structure of a simple Async. Task Class
private class My. Async. Task extends Async. Task<X, Y, Z> protected void on. Pre. Execute(){ This method is executed before starting the new Thread. Use on. Pre. Execute to initialize and setup. NOTE: The return type is void and there are no parameters. } protected Z do. In. Background(X. . . x){ Place all the background workload in this method. do. In. Background() uses a parameter of type X (array of objects from the type “X”). do. In. Background() returns an object of type “Z”. } } } protected void on. Progress. Update(Y y){ on. Progress. Update is called using the method publish. Progress(y). This method is called when you want to update UI components in the Activity layout. on. Progress. Update () uses a parameter of type Y. protected void on. Post. Execute(Z z){ on. Post. Execute is called after do. In. Background() has completed its work. NOTE: do. In. Background() sends a value of type Z to on. Post. Execute().
Further Explanation of Parameters Refer to the previous slide. • X : The type of the input variables value you want to set to the background process. This can be an array of objects. • Y : The type of the objects you are going to enter in the on. Progress. Update method. • Z : The type of the result from the operations you have done in the background process.
Another Example
Instantiating and executing an Async. Task An Async. Task object must be instantiated and called with the execute() method. Example: My. Async. Task my. Task = new My. Async. Task (); my. Task. execute(x); //x is an input parameter of the type X.
private class Heavy. Duty. Task extends Async. Task <double, Void, int> { @Override protected void on. Pre. Execute() { // Perform setup - runs on user interface thread To } execute this code: new Heavy. Duty. Task(). execute(); @Override protected int do. In. Background(double…d) { // Insert heavy duty task work here! Runs in the background. } @Override protected void on. Progress. Update(Void… param) { // Update user with progress information or similar - runs on UI Thread } @Override protected void on. Post. Execute(int result) { // Update user interface. Runs on the UI Thread } }
Rules for using Async. Task 1. Do not call the methods on. Pre. Execute, do. In. Background and on. Post. Execute manually. These are callback methods and will be performed automatically done by the system. 2. You cannot call an Async. Task inside another Async. Task or Thread. The call of the method execute must be done in the UI Thread. 3. The method on. Post. Execute is executed in the UI Thread (here you can call another Async. Task!). 4. The input parameters of the task can be an Object array, this way you can put whatever objects and types you want.
Final Review Three ways to perform work in the background: • Native Threads: No Handlers • Threads with Handlers (Message. Queue) • Async. Task
Native Threads (No Handlers) Pros: Android supports standard (native) Java Threads. This means that you can use native threads to execute actions in the background. Cons: 1. The only limitation is that you cannot directly update the UI from a background thread. 2. If you need to update the UI from a background thread you need to use a Hander or a Asyn. Task.
Threads with Handlers (Message. Queue) Pros: 1. Android supports the pairing of standard Java Threads with Handlers. 2. Messages can be passed back to the UI Thread by using a Handler's post method. 3. A Handler allows you to communicate back with the UI thread from a standard background thread. This is useful in android as Handler can send and process Message and Runnable objects associated with a thread’s Message. Queue. Each Handler instance is associated with a single thread and that thread’s message queue. When a new Handler is created, it is bound to the thread/message queue of the thread that is creating it. 4. This method is useful for long-running heav-yduty tasks. Cons: Requires more code and is not always useful for very short tasks that do not impact the UI.
Async. Task Pros: 1. The Async. Task class encapsulates the creation of a background process and the synchronization with the UI Thread. 2. Async. Task is easy to use because it allows programmers to perform background operations and publish results on the UI thread without having to manipulate threads and handlers. Cons: 1. By default, an app pushes all of the Async. Task objects it creates into a single thread. 2. No always useful for very long tasks.
- Slides: 26