开发者

Command Not working in separate thread in J2me

开发者 https://www.devze.com 2022-12-27 21:51 出处:网络
I am creating a bluetooth application. I created a simple midlet with a exit command and I create a thread for finding the service and discovering the device. While doing so it displays a animated sc

I am creating a bluetooth application.

I created a simple midlet with a exit command and I create a thread for finding the service and discovering the device. While doing so it displays a animated screen on which I added the parent commandListener for exit command. After successful connection both user is represented with greetings(Current screen calls the parent Display method setCurrent for displaying itself). This screen also have CommandListener set to the parent. Now I want to add few more commands. I Implemented the CommandLIstener Interface in this class, added few commands but the commands are not working. I don't know what's wrong. I am giving you Code snippets to fully describe my issue:

package name
Imports here

public class MyMidlet extends MIDlet implements
CommandListener {

public CommandListener theListener;

public Display theDisplay;
public Command exitCommand;

public MyMidlet() {
    // Retrieve the display for this MIDlet
    //Create the initial screen
}

public void startApp() throws MIDletStateChangeException {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {

    // Determine if the exit command was selected
    if (c == exitCommand) {
        //End application here
        notifyDestroyed();
    } else {
        //Start the new thread here
    }
}
}

Now here is the code for the class which is invoked by the above midlet in a separate thread;

package here;
imports here
public class MyService implements Runnable, CommandListener {

private MyMidlet parent;
private StreamConnection conn;
private OutputStream output;
private InputStream input;

public Command sendCommand;

private TextField messageToSend
Form form;

public BChatService(boolean isServer, BChatMidlet 开发者_开发问答parent) {
   //some stuff here
this.parent = parent;
}

public void run() {
    //function for showing animation here

try {
input = conn.openInputStream();
output = conn.openOutputStream();
} catch (IOException e) {
displayError("IO Error",
"An error occurred while opening " +
"the input and output streams" +
"(IOException: " + e.getMessage() + ")");
try {
conn.close();
} catch (Exception ex) {
}
return;
}
// Create the Form here when service is discoverd and greets the users


    Command sendCommand = new Command("Send", Command.ITEM, 2);
    exitCommand = new Command("Exit", Command.EXIT, 1);
    form.addCommand(exitCommand);
    form.addCommand(sendCommand);

parent.theDisplay.setCurrent(form);

form.setCommandListener(this);

     public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            // End the game
            parent.destroyApp(true);
            parent.notifyDestroyed();
        }
        if(c == sendCommand)  {
          form.append("SOme text here");

        }

    }
}

When I select the Send command, the string doesn't append in form neither exit command works.

What can be the possible cause for it?

I need to implement this functionality... Is there any other way to achieve this?


Are you sure the problem is with the command? What i know is that the call openInputStream blocks the entire program until it gets an answer or reachs the timeout, even if you run them on diferent threads (dont know why). Is the connection realy established?

0

精彩评论

暂无评论...
验证码 换一张
取 消