How come I can't stop this thread from sleeping?
Hello!I'm playing around with J2ME and tries to do a modal Alert that returns the value of the user input.
I do not think it's possible from just the class in the API.
How ever. In one thread I start and show a Alert like:
ConfirmAlert confirmAlert = new ConfirmAlert("Please confirm!", "Ok", "Cancel", "This will reset The database");
int i = confirmAlert.showAlert(this);
closeAlert()
The confirm alert just looks like:
public class ConfirmAlert extends MyAppAlert
{
private Command cancel;
private Command confirm;
public ConfirmAlert(String t, String confirmText, String cancelText, String message )
{
super(t, message);
cancel = new Command( cancelText, Command.CANCEL, 2 );
confirm = new Command( confirmText, Command.OK, 1 );
setString(message);
addCommand(cancel);
addCommand(confirm);
setCommandListener(this);
setTimeout(Alert.FOREVER);
}
}
The Alert is shown and in the extended method I start sleep the current thread.
But even though I change the value of the state the thread does not stop sleeping.
The state has the same value.
Below is my code with the loop that I'm hope that I may stop by pushing a command and change the state.
I do not see where I go wrong. I also hope I have posted enough info for you guys.
Best regards
Fredrik
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class MyAppAlert extends Alert implements CommandListener
{
int state = 0;
public MyAppAlert(String t, String message)
{
super(t, message, null, AlertType.WARNING);
setTimeout(Alert.FOREVER);
}
public int showAlert(InfoCanvas infoCanvas)
{
infoCanvas.showAlert(this);
Thread thread = Thread.currentThread();
try
{
while( getState() == 0 )
{
System.out.println("State " + state);
thread.sleep( MyAppMIDlet.SLEEPTIME );
}
}
catch( InterruptedException e )
{
e.printStackTrace();
}
return state;
}
public void commandAction( Command c, Displayable d )
{
setState(c.getCommandType());
System.out.println(getState() + " Command "+c.getCommandType());
}
public synchronized int getState()
{
return state;
}
public synchronized void setState(int s)
{
state = s;
}
}
