想實現一個UI,提示用戶輸入PIN碼,輸入完後函數返回用戶輸入的PIN碼。代碼如下:
public class PinInputForm
extends Form
implements CommandListener
{
......
public String GetInputPIN(Display display)
{
//show dialog
Displayable prevUi= display.getCurrent();
display.setCurrent(this);
//wait for "ok"
synchronized(this){
try{
wait();
}catch(Exception e){}
}
display.setCurrent(prevUi);
return m_strRet;
}
public void commandAction(Command c, Displayable s)
{
if(c==m_CancelCommand)
{
m_strRet=null;
synchronized(this){
notify();
}
}
else if(c==m_OkCommand)
{
m_strRet= m_txtBoxPIN.getString();
synchronized(this){
notify();
}
}
}
}
這在Windows下很好實現,只要一個模式對話框就可以。但發現在J2ME環境下,竟然無法實現。原因是:J2ME爲Midlet提供一個UI線程,且僅有一個。如果用戶在該線程下阻塞,Midlet就得不到再次調度
系統原理如下:
經過調用Connet後:
如果在connet處阻塞的話,系統線程就出不來了