最近要考试了,复习太无聊了,学习了一会J2ME,感觉比J2SE简单的多了,一直以来我多没有很好的解决界面问题(很不爽呀)。学了一会儿J2ME,感觉很简单,乘热打铁写了一个发送短信的小程序,还有几个问题没有解决(复制,粘贴, 从电话薄中导入联系人)。有兴趣的人可以自己写(查看一下手机厂商提供的API就能解决了)。下面是代码:
欢迎界面:
package com.ui;
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Welcome extends MIDlet implements CommandListener{
/******************************************************/
/* 欢迎界面 */
/******************************************************/
private Display display;
private Form form = new Form("");
private Command cmdNextPage;
private Command cmdExit;
private Image image;
private ImageItem welcomItem;
public Welcome() {
try {
image = Image.createImage("/rss.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
welcomItem = new ImageItem("欢迎使用本程序", image, Item.LAYOUT_CENTER, "");
cmdExit = new Command("退出", Command.EXIT, 1);
cmdNextPage = new Command("继续", Command.SCREEN, 1);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
display.setCurrent(form);
form.addCommand(cmdExit);
form.addCommand(cmdNextPage);
form.setCommandListener(this);
form.append(welcomItem);
}
public void commandAction(Command cmd, Displayable dis) {
if(cmd == cmdExit) {
this.notifyDestroyed();
}else if(cmd == cmdNextPage) {
WriteMsg writemsg = new WriteMsg("写信息");
display.setCurrent(writemsg);
}
}
}
写短信界面:
package com.ui;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.TextField;
import com.classes.*;
public class WriteMsg extends Form implements ItemCommandListener, CommandListener {
/*****************************************************************/
/* 编辑短信 */
/*****************************************************************/
private Command cmdDel; // 删除单个字符
private Command cmdSend; // 发送
private Command cmdInsertLinkMan; // 增加联系人
private Command cmdCopy; // 自由复制
private Command cmdPaste; // 粘贴
private Command cmdExit; // 退出
private TextField tel; //电话号码输入框
private TextField text; //短信内容输入框
public WriteMsg(String title) {
super(title);
this.setCommandListener(this);
cmdDel = new Command("删除", Command.CANCEL, 1);
cmdSend = new Command("发送", Command.SCREEN, 1);
this.addCommand(cmdSend);
cmdInsertLinkMan = new Command("增加联系人", Command.SCREEN, 1);
this.addCommand(cmdInsertLinkMan);
cmdCopy = new Command("复制", Command.SCREEN, 1);
this.addCommand(cmdCopy);
cmdPaste = new Command("粘贴", Command.SCREEN, 1);
this.addCommand(cmdPaste);
cmdExit = new Command("退出", Command.SCREEN, 1);
this.addCommand(cmdExit);
tel = new TextField("收件人", "", 15, TextField.PHONENUMBER);
tel.addCommand(cmdDel);
tel.setItemCommandListener(this);
this.append(tel);
text = new TextField("", "", 300, TextField.ANY);
text.setLayout(Item.LAYOUT_2);
text.addCommand(cmdDel);
text.setItemCommandListener(this);
this.append(text);
}
public void commandAction(Command cmd, Displayable dis) {
if(cmd == cmdSend) {
SendMsg sendMsg = new SendMsg(text.getString());
if(sendMsg.sendMsg(tel.getString())) {
System.out.println("发送成功");
}else {
System.out.println("发送失败");
}
}else if(cmd == cmdInsertLinkMan) {
}else if(cmd == cmdCopy) {
}else if(cmd == cmdPaste) {
}else if(cmd == cmdExit) {
System.exit(0);
}
}
public void commandAction(Command cmd, Item item) {
if(item == tel) {
int offset = tel.getCaretPosition();
if(offset >= 1) {
tel.delete(offset-1, 1);
}
}else if(item == text) {
int offset = text.getCaretPosition();
if(offset >= 1) {
text.delete(offset-1, 1);
}
}
}
}
发送短信的类:
package com.classes;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
public class SendMsg {
private String content; //短信内容
public SendMsg(String content) {
this.content = content;
}
public boolean sendMsg(String tel) {
boolean boo = false;
String address = "sms://"+tel; //目标地址
MessageConnection conn = null;
try {
conn = (MessageConnection) Connector.open(address); //连接这个address端口,也就是目标地址
TextMessage msg = (TextMessage) conn
.newMessage(MessageConnection.TEXT_MESSAGE); //创建一个要被发送的消息实例
msg.setPayloadText(content); //设置信息内容
conn.send(msg); //发送短信
boo = true; //发送成功
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return boo;
}
}
在本人手机上测试成功了。祝各位好运!!!