【发布时间】:2023-03-03 19:06:01
【问题描述】:
我正在创建一个蓝牙应用程序。
我使用退出命令创建了一个简单的 midlet,并创建了一个用于查找服务和发现设备的线程。这样做时,它会显示一个动画屏幕,我在该屏幕上添加了退出命令的父 commandListener。成功连接后,两个用户都会用问候语表示(当前屏幕调用父 Display 方法 setCurrent 来显示自身)。此屏幕还将 CommandListener 设置为父级。现在我想添加更多命令。我在此类中实现了 CommandLIsener 接口,添加了一些命令,但命令不起作用。我不知道怎么了。我给你代码 sn-ps 来完整描述我的问题:
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
}
}
}
下面是上述 midlet 在单独线程中调用的类的代码;
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");
}
}
}
当我选择发送命令时,字符串不会附加到表单中,退出命令也不起作用。
可能的原因是什么?
我需要实现这个功能...还有其他方法可以实现吗?
【问题讨论】:
-
@RishiPatel,请改进格式。
-
它的格式正确,但是当我粘贴它时,我做了一些格式化,它变成了这样。