- 积分
- 21
- 实力分
- 点
- 金钱数
- 两
- 技术分
- 分
- 贡献分
- 分
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
在国外的网站上找到的
好像是JAVA的源代码
package cz.kruch.test;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Item;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.TextMessage;
import java.io.IOException;
public final class CbsTestMIDlet extends MIDlet implements MessageListener, Runnable {
private MessageConnection connection;
private Form form;
private int count;
protected void startApp() throws MIDletStateChangeException {
Display display = Display.getDisplay(this);
form = new Form("CBS Test");
display.setCurrent(form);
form.append(appendLine("Open channel..."));
try {
this.connection = (MessageConnection) Connector.open("cbs://:221", Connector.READ);
this.connection.setMessageListener(this);
form.append(appendLine("Waiting..."));
} catch (Throwable t) {
form.append(appendLine(t.toString()));
display.setCurrent(new Alert("Error", t.toString(), null, AlertType.ERROR), form);
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean b) throws MIDletStateChangeException {
// TODO quit politely
}
public void notifyIncomingMessage(MessageConnection messageConnection) {
if (++count > 24) { // 24 msgs max - stop listening...
try {
this.connection.setMessageListener(null);
} catch (IOException e) {
// ignore
}
} else {
(new Thread(this)).start();
}
}
public void run() {
try {
Message message = connection.receive();
if (message instanceof TextMessage) {
form.append(appendLine("[" + ((TextMessage) message).getPayloadText() + "]"));
} else {
form.append(appendLine("[<binary msg>]"));
}
} catch (IOException e) {
form.append(appendLine("[" + e.toString() + "]"));
}
}
private Item appendLine(String line) {
StringItem item = new StringItem(null, line);
item.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER);
return item;
}
} |
|