【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“BinMsg.java”一文,供大家参考学习!
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.*;
public class BinMsg extends MIDlet
implements CommandListener, MessageListener,Runnable {
/** 退出命令按钮 */
private Command cmdExit;
/** 发送消息的命令按钮 */
private Command cmdSendMsg;
//Display管理
private Display display;
private Form form;
/** 消息内容 */
private TextField tfMsgText;
/** 用于输发送到的电话号码 */
private TextField tfPhoneNumber;
/** 用于接收消息 */
private MessageConnection recieveConn;
//在收到二进制消息时,以下一个表单和两个按钮用于显示提示信息
Command cmdRead ;
Command cmdReturn;
Form formNotice;
//用于显示收到的图像
BinMsgCanvas bmc = new BinMsgCanvas();
/** 保存接收到的二进制消息 */
private BinaryMessage bm;
public BinMsg() {
display = Display.getDisplay(this);
cmdExit = new Command("退出程序", Command.EXIT, 1);
cmdSendMsg = new Command("二进制格式发送", Command.SCREEN, 2);
tfMsgText = new TextField("请输入消息内容:", "", 255, TextField.ANY);
tfPhoneNumber = new TextField("请输入接收号码:", "", 255,
TextField.PHONENUMBER);
}
/**
* 开始运行MIDlet
*/
public void startApp() {
//创建消息连接
try {
recieveConn = (MessageConnection)Connector.open
("sms://:5008");
//
注册接收消息的监听器
recieveConn.setMessageListener(this);
} catch (IOException ioe) {
System.out.println("不能进行接收消息连接:" + ioe.toString());
}
//初始化收到二进制消息时的提示窗口
cmdRead = new Command("阅读消息", Command.SCREEN, 2);
cmdReturn = new Command("返回", Command.STOP, 1);
formNotice = new Form("收到新二进制消息");
formNotice.addCommand(cmdRead);
formNotice.setCommandListener(this);
//初始化显示图像的画布
bmc.addCommand(cmdReturn);
bmc.setCommandListener(this);
//初始化主窗口
form = new Form("接收/发送消息
演示 - 接收端口为5008");
form.append(tfMsgText);
form.append(tfPhoneNumber);
form.addCommand(cmdExit);
form.addCommand(cmdSendMsg);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
/**
* 处理命令按钮事件
*/
public void commandAction(Command c, Displayable s) {
String label = c.getLabel();
if (label.equals("退出
程序")) {
destroyApp(false);
} else if (label.equals("二进制格式发送")) {
//检查电话号码是否存在
String pn = tfPhoneNumber.getString();
if (pn.equals("")) { //注意如果使用pn==""会不起作用
Alert alert = new Alert("发送消息错误",
"请输入接收的电话号码", null,
AlertType.ERROR);
alert.setTimeout(2000);
display.setCurrent(alert, form);
AlertType.ERROR.playSound(display);
} else {
try {
new Thread(this).start();
} catch (Exception exc) {
exc.printStackTrace();
}
}
} else if (label.equals("阅读消息")) {
//显示二进制消息内容
if (bm != null) {
bmc.setPicData(bm.getPayloadData());
display.setCurrent(bmc);
bmc.repaint();
}
} else if (label.equals("返回")) {
//返回主界面
bm = null;
display.setCurrent(form);
}
}
/**
* 定义线程的执行方法,给指定号码发送二进制信息
*/
public void run() {
try{
//地址
String address = "sms://+" + tfPhoneNumber.getString();
//建立连接
MessageConnection conn=(MessageConnection)Connector.open(address);
//设置短信息类型为二进制类型
BinaryMessage msg = (BinaryMessage)conn.newMessage(
MessageConnection.BINARY_MESSAGE);
//设置消息地址
msg.setAddress(address);
//设置信息内容
msg.setPayloadData(tfMsgText.getString().getBytes());
//发送消息
conn.send(msg);
}catch(Exception e){
System.out.println("发送短消息错误:" + e.toString());
}
}
/**
* 接收消息
*/
public void notifyIncomingMessage(MessageConnection conn) {
System.out.println("收到了一条新信息");
Message msg = null;
//读取消息
try {
msg = conn.receive();
} catch (Exception e) {
//处理读取消息时的异常
System.out.println("读取消息错误:" + e);
}
//处理读取的二进制消息
if (msg instanceof BinaryMessage) {
bm = (BinaryMessage)msg;
formNotice.deleteAll();
formNotice.append("发送时间:\n" +
bm.getTimestamp().toString() + "\n");
formNotice.append("消息发送方:\n" + bm.getAddress() + "\n");
display.setCurrent(formNotice);
}
}
class BinMsgCanvas extends Canvas {
byte[] picData = null;
//构造二进制消息显示画布,data 必须为二进制图形数据
p