Command Not working in separate thread in J2me.
Posted
by RishiPatel
on Stack Overflow
See other posts from Stack Overflow
or by RishiPatel
Published on 2010-04-25T13:26:39Z
Indexed on
2010/04/25
13:33 UTC
Read the original article
Hit count: 196
j2me
|multithreading
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 dont whats wen wrong. I am giving u Code snippets to fully describle 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() {
//functino 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 doesnt 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??
© Stack Overflow or respective owner