Hi all
I am starting with J2ME and I get problems very quickly.
I have a midlet which displays "Hello".
When I push a command I want the midlet creating a thread which says "something else".
If, in this new thread, I push something like back command, I want the thread to be killed and give the hand back to the midlet.
else, I want to wait until the command is activated.
this is my code :
and my thread is like this :Code:public class Visu extends MIDlet implements CommandListener { public static final Form form = new Form("Test"); Form currentForm = form; public static TextField TextField = new TextField("","",32,0); public static final Command readInfoCmd = new Command("Read info" , Command.ITEM , 0 ), Command exitCmd = new Command("Exit" , Command.EXIT , 1 ); boolean running = false; private StringItem screenText = new StringItem(null, ("\n\n")); ThreadFactory currentThread = null; TestThread TestThread; public Visu() { super(); } [...] public void commandAction(Command c, Displayable d) { if (c == exitCmd) exit(); else if(c == readInfoCmd) { TestThread = new TestThread(false); currentThread = TestThread; displayT(); } } private void displayT() { currentForm = currentThread.getForm(); currentThread.setCommandListener(this); currentThread.setDisplay(Display.getDisplay(this)); if(currentThread !=null) { currentThread.start(); Display.getDisplay(this).setCurrent(form); } else Display.getDisplay(this).setCurrent(form); } private void exit() { try { destroyApp(false); notifyDestroyed(); } catch (MIDletStateChangeException ex) { screenText.setText(screenText.getText() + ex.toString()); } } }
(threadFactory is an abstract class which extends Thread).Code:public class TestThread extends ThreadFactory { boolean running = false; boolean forever = false; public TestThread(boolean forever) { super(); this.forever = forever; currentForm = new Form("In the thread"); } [...] public void commandAction(Command c, Displayable d) { if (c == backCmd) { display("Bye Bye !"); currentForm = Visu.form; running = false; } } public void run() { running = true; display("The thread is running"); while(running == true) { } } protected void stopTest() { display ("back to main menu"); running = false; } }
So if I push "Read Info" in the midlet, I correctly go into my thread.
But When I push "back" into the thread, I never see "back to main menu" and nothing else can be done excepted exit the application.
Questions :
- Do you see where could be the problem ?
- Do threadFactory (or TestThread) has to implement CommandListener to detect actions on Command ?
- The code is from an other guy and I don't get the use of boolean forever, do you understand futur advantages ?
- Do you know some easy "how to" about threads in J2ME (or boooks - I have "Beginning J2ME Plateform From Novice to Professional", from Sing Li and Jonathan Knudsen, but it never explain nicely how to do, I don't like very much this book) ?
(The final objectif of this midlet is to have some items in a menu and create a thread each time a item is select, then perform actions and finally go back to the main menu of the midlet and have the choice again between all commands).
Thanks for taking time !


Reply With Quote


