Game Development Reference
In-Depth Information
mHandler.post(new Runnable() {
public void run() {
mView.setImageBitmap( mBitmap);
}
});
}
Note that because this handler fires from a non-UI thread, you cannot set the pixels directly into the
ImageView , but must use an android.os.Handler to post a Runnable to the message queue:
Handler.post(new Runnable() {
public void run() {
// Code that updates the UI goes here
}
});
Note A handler allows you to 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
you create a new handler, it is bound to the thread and message queue of the thread that is creating it. Always use
a handler when updating UI widgets from a non-UI thread!
Message Updates
The message updates handler receives native messages, which are very helpful for debugging. Listing 7-8
shows this handler, which logs the text to the Android console.
Listing 7-8. Message Update Handler
/**
* Fires on DSO message
*/
public void OnMessage(String text, int level) {
Log.d(TAG, "**Doom Message: " + text);
}
Fatal Error Handler
The fatal error handler deals with unrecoverable errors. This means displaying a message to the user and
exiting gracefully. There are many things that can cause unrecoverable errors, such as code bugs,
corrupted game files, I/O errors, and network failures.
Listing 7-9 shows the way Doom deals with this situation. It uses a message handler to display a
message box to the user (remember that this method fires from a non-UI thread, where all UI widget
access must go through an OS handler). It then waits for a while so the user can read the message, and
finally exits gracefully.
Search WWH ::




Custom Search