Worker thread unable to update the UI on Motorola V3
I'm working on a J2ME application that should be ported to different vendors phones. So far, the application is working fine, except on the Motorola V3 (this is the only Motorola phone I have tried).
The problem is that when I start a new Thread, and when the thread tries to update the graphics (by calling repaint()), the graphic system is stack.
Some useful info:
- I'm obtaining the display in the constructor of the Midlet, and reusing it during the appl开发者_开发问答ication lifespan.
- There is only one class extending Canvas. Every drawing happens here. When I want the graphics repainted, I call the repaint() method, either from the UI thread or from another Worker thread. As far as I know, every thread can access the UI without problems.
- I tried a fix where, in the beginning of the app, I take the name of the running (UI) thread and store it for later use. Before repaint() is called, I check if the current thread's name equals to the UI thread's name. If it odes, then I do repaint()
, else I do:
display.callSerially(new Runnable() {
public void run() {
repaint();
}
});
Can you point to me any specifics in the Motorola's implemenation of J2ME? How can I solve this problem with threads updating the UI?
Thanks, Zlatko
Unlike Swing, the MIDP user interface APIs are thread-safe. This means you don't have to use callSerially to force things to run on the UI thread. There are reports that callSerially doesn't work well on some Motorola devices, so your best bet is to just call repaint by itself.
Note that repaint is just a suggestion to the system to do the repainting. If you want to force the repaints to occur immediately, you have to call the serviceRepaints method.
A good discussion of MIDP UI event handling can be found here.
精彩评论