The BlackBerry UI API is single-threaded. This means that all UI updates and events are handled by the same thread—or more precisely, must be done while holding the event lock, which most of the time is held by the UI thread. It also has a couple of implications for BlackBerry applications: other threads can’t directly access the UI without explicitly acquiring the event lock (an exception will be thrown if you try), and if you perform an operation on the event thread that takes a long time,the entire user interface will pause while that operation is taking place.The message to take away from all this is to get comfortable with using at least one or two other threads in your applications.
NOTE: We’re not going to be doing heavy concurrency in this book, so if you’re understand what a thread is and how to create and start one, you’ll be fine. However, if you’re not comfortable with the idea of threading at all, or have never used threads in your programming, I recommend you take a look at the Java SE threading API.
Knowing When Your Application Is on the Event Thread
You can always tell if your code is being executed by the event thread by calling UiApplication.isEventDispatchThread(). Generally though, you shouldn’t need to invoke this.A good rule of thumb for determining if you’re on the event thread is: If your code was invoked more or less directly from the run method of a Thread that you created, you’re not on the event thread. If the code was invoked by the system in response to user input (for example,a menu item or a button click), you’re on the event thread.
Updating the UI from Other Threads
Of course, many times, you want an event in another thread to be reflected in the UI, for example, to show progress of a long network activity like a file download. To do that, there are a couple of methods. The first is to use UiApplication. invoke Later or UiApplication. invoke And Wait to tell the UI thread to run some code on the event thread at the next available opportunity. The second method is to acquire the UI event lock by synchronizing on the object returned by UiApplication. getEventLock(). We’ll explore the first method by modifying Hello World to start a thread that adds a message to the main screen’s Label field every 5 seconds.
NOTE: Both invokeLater and invokeAndWait do the same thing—queue an instance of java.lang.Runnable to be executed on the event thread. The difference is that invokeLater returns immediately, while invokeAndWait doesn’t return until the run method of your Runnable has finished executing and, therefore, blocks the thread that calls it First, let’s change labelField to be a member variable instead of a variable local to the constructor, and add a method to append text to it:
Because appendLabelText calls LabelField.setText, the call can only be made from the event thread. If you attempt to call this method directly from another thread, an exception will be thrown.Now, we’ll define the thread class that will actually do the updating. It will loop from 1 to 10. In each iteration, it will wait 5 seconds and then add some text to the LabelField. Create a new class called MainScreenUpdaterThread that extends java.lang.Thread. The full source code follows:
To actually update the UI, we’re using an anonymous inner class, which is a class that we define at the point where we instantiate it. Our anonymous inner class calls the one method that needs to be called on the event thread—appendLabelText (which calls LabelField.setText). We’ll start our thread in the HelloWorldMainScreen constructor as follows:
Finally, running this application will produce the output shown in Figure.
A few updates from our thread
Using the Event Lock
What about the second method of updating the UI from another thread? With a simple modification to MainScreenUpdaterThread we can do that too. Change the run method of MainScreenUpdaterThread to the following:
The application should run exactly the same way as in the previous example.This example application is admittedly a bit contrived, but it’s important to understand the concept of updating the UI from a different thread, as this concept will be applied throughout most BlackBerry applications you create.
BLACKBERRY Related Interview Questions
|J2ME Interview Questions||BLACKBERRY Interview Questions|
|Android Interview Questions||Retail Interview Questions|
|Telecommunication Project Management Interview Questions||Mobile Testing Interview Questions|
|Telemarketing Interview Questions||Mobile Application Testing Interview Questions|
|Mobile Marketing Interview Questions||Telecom Billing Interview Questions|
|Mobile Application Architect Interview Questions|
Setting The Stage
What Makes A Blackberry Application?
User Interface Basics
Beyond The Basics Of User Interfaces
Hello Out There! Making A Network-enabled Application
Where Am I? Using Location-based Services
Getting Your App Out There: Packaging And Publishing
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.