Android background thread and UI thread communication instance _android

Source: Internet
Author: User

This section shows you how to send data to the UI line Chengri objects in a task that allows you to work in a background thread and then display the results on the UI thread.

Defines a handler in the UI thread

Handler is part of the Android system's threading management framework. A handler object receives the message and runs the code to process the message. Normally, you create a handler for a new thread, but you can also create a handler for an existing thread. When you connect handler to the UI thread, the code that processes the message runs on the UI thread.

Instantiate the handler object in the constructor of the class that created the thread pool and save it in a global variable. Instantiated with the handler (Looper) method, connected to the UI thread, and constructed using Looper objects, as well as part of the Android thread management framework. The Looper class has a static method Getmainlooper () You can get the Looper object for the UI thread. Such as:

Copy Code code as follows:

Private Photomanager () {
...
Defines a Handler object that ' s attached to the UI thread
Mhandler = new Handler (Looper.getmainlooper ()) {
...

In handler, cover Handlemessage (). The Android system calls this method when a handler-managed thread receives a new message. All handler objects of a specified thread will receive the same message.

Copy code code as follows:

/*
* Handlemessage () defines the operations to perform
* The Handler receives a new message to process.
*/
@Override
public void Handlemessage (message inputmessage) {
Gets the image task from the incoming message object.
Phototask Phototask = (phototask) inputmessage.obj;
...
}
...
}
}

Moving data from a task to a UI thread

To move data to the UI thread's object from a background thread's task, first save the UI object referenced to the data and Task object, and then pass the Task object and status code to the handler object. In this object, send a message containing the status and Task object to handler. Because handler runs on the UI thread, it can move data to the UI object.

Storing data in a Task object

For example, this is a runnable that runs in the background thread, which resolves bitmap and saves it to its parent object. Runnable Saves the status code decode_state_completed at the same time.

Copy Code code as follows:

A class that decodes photo the files into bitmaps
Class Photodecoderunnable implements Runnable {
...
Photodecoderunnable (Phototask downloadtask) {
Mphototask = Downloadtask;
}
...
Gets the downloaded byte array
byte[] Imagebuffer = Mphototask.getbytebuffer ();
...
Runs the code for this task
public void Run () {
...
Tries to decode the image buffer
Returnbitmap = Bitmapfactory.decodebytearray (
Imagebuffer,
0,
Imagebuffer.length,
Bitmapoptions
);
...
Sets the ImageView Bitmap
Mphototask.setimage (RETURNBITMAP);
Reports a status of "completed"
Mphototask.handledecodestate (decode_state_completed);
...
}
...
}
...

Phototask also contains a ImageView reference to display the bitmap. Although references to bitmap and ImageView are in the same object, you cannot directly let ImageView show bitmap because it is not on the UI thread.

Send status along the object hierarchy

Phototask holds a reference to a View object that decodes data and displays data, receives a status code from Photodecoderunnable, and passes along a line Cheng Chili referenced objects and handler instances.

Copy Code code as follows:

public class Phototask {
...
Gets a handle to the object that creates the thread pools
Sphotomanager = Photomanager.getinstance ();
...
public void handledecodestate (int state) {
int outstate;
Converts the decode state to the overall state.
Switch (state) {
Case photodecoderunnable.decode_state_completed:
Outstate = Photomanager.task_complete;
Break
...
}
...
Calls the generalized state method
Handlestate (outstate);
}
...
Passes the state to Photomanager
void handlestate (int state) {
/*
* Passes a handle to this task and the
* current ' to ' class that created
* The thread pools
*/
Sphotomanager.handlestate (this, state);
}
...
}

Moving data to the UI

Photomanager receives a handle to the status code and the Phototask object from the Phototask object. Because the state is Task_complete, create a message containing the status and Task object, sent to handler.

Copy Code code as follows:

public class Photomanager {
...
Handle status messages from tasks
public void Handlestate (phototask phototask, int state) {
Switch (state) {
...
The task finished downloading and decoding the image
Case Task_complete:
/*
* Creates a for the Handler
* With the State and the Task object
*/
Message Completemessage =
Mhandler.obtainmessage (state, Phototask);
Completemessage.sendtotarget ();
Break
...
}
...
}

Eventually, Handler.handlemessage () checks the status code for each incoming message. If the status code is Task_complete, the task is complete, and the Phototask object in the message contains bitmap and ImageView. Because the Handler.handlemessage () runs on the UI thread, It can safely set bitmap for ImageView.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.