×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    12

    midlet and thread - just begin

    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 :
    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());
    		}
    	}
    }
    and my thread is like this :

    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;
            }
    }
    (threadFactory is an abstract class which extends Thread).

    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 !

  2. #2
    Registered User
    Join Date
    Dec 2009
    Location
    Bangalore/Mysore, Karnataka, India.
    Posts
    43

    Re: midlet and thread - just begin

    Quote Originally Posted by gilloull View Post
    Hi all
    - Do threadFactory (or TestThread) has to implement CommandListener to detect actions on Command ?
    1) Yes. you have to implement CommandListener in TestThread. And setCommandListener of the form to it.
    -Roopesh.
    (http://trivialdust.blogspot.com/)

Similar Threads

  1. Pausing a thread when the user leaves the MIDlet
    By Woody_FX in forum Mobile Java General
    Replies: 17
    Last Post: 2010-03-26, 09:23
  2. Can midlet run in the background thread?
    By crazyII in forum Mobile Java General
    Replies: 9
    Last Post: 2010-01-19, 16:44
  3. start midlet by bluetooth on S40
    By peba25 in forum Mobile Java Networking & Messaging & Security
    Replies: 15
    Last Post: 2009-11-05, 09:35
  4. Network communication and Server-Client Thread errors.
    By mujtaba_muji in forum Mobile Java General
    Replies: 0
    Last Post: 2007-06-09, 07:33
  5. How to create two instances of midlet
    By marquito99 in forum Mobile Java General
    Replies: 0
    Last Post: 2004-04-30, 01:33

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •