Java ME 短消息

发短消息

package com.zeph.s40.sms; import java.io.IOException; import javax.microedition.io.Connector; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; 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.TextField; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.wireless.messaging.MessageConnection; import javax.wireless.messaging.TextMessage; public class S40MIDletSMS extends MIDlet implements CommandListener { private Display display = null; private Form mainForm = null; private TextField phonenumTextField = null; private TextField portnumTextField = null; private TextField messageTextField = null; private Command exitCommand = null; private Command sendSmsCommand = null; public S40MIDletSMS() { // TODO Auto-generated constructor stub display = Display.getDisplay(this); mainForm = new Form("SMS"); phonenumTextField = new TextField("PHONE NUM", "", 15,TextField.NUMERIC); portnumTextField = new TextField("PORT NUM", "", 10, TextField.NUMERIC); messageTextField = new TextField("MESSAGE", "Input Your Message Here",100, TextField.ANY); exitCommand = new Command("EXIT", Command.BACK, 1); sendSmsCommand = new Command("SEND", Command.OK, 1); mainForm.addCommand(sendSmsCommand); mainForm.addCommand(exitCommand); mainForm.append(phonenumTextField); mainForm.append(portnumTextField); mainForm.append(messageTextField); mainForm.setCommandListener(this); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub } protected void pauseApp() { // TODO Auto-generated method stub } protected void startApp() throws MIDletStateChangeException { // TODO Auto-generated method stub display.setCurrent(mainForm); } public void commandAction(Command command, Displayable displayable) { // TODO Auto-generated method stub if (command == sendSmsCommand) { send(); } else if (command == exitCommand) { try { destroyApp(true); notifyDestroyed(); } catch (MIDletStateChangeException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private void send() { MessageConnection mc = null; String number = phonenumTextField.getString(); String port = portnumTextField.getString(); String content = messageTextField.getString(); String address; if (number.equals("")) { Alert alert = new Alert("ERROR", "Input a right number!", null,AlertType.ERROR); alert.setTimeout(3000); display.setCurrent(alert, mainForm); return; } else if (port.equals("")) { address = "sms://+" + number; } else { address = "sms://+" + number + ":" + port; } try { mc = (MessageConnection) Connector.open(address); TextMessage msg = (TextMessage) mc.newMessage(MessageConnection.TEXT_MESSAGE); msg.setPayloadText(content.toString()); mc.send(msg); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (mc != null) { mc.close(); mc = null; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }


收消息

package com.zeph.s40.sms; import java.io.IOException; import java.io.InterruptedIOException; import javax.microedition.io.Connector; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; 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.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.wireless.messaging.Message; import javax.wireless.messaging.MessageConnection; import javax.wireless.messaging.MessageListener; import javax.wireless.messaging.TextMessage; public class S40MIDletSMS2 extends MIDlet implements CommandListener, MessageListener { private Display display = null; private MessageConnection mc = null; private Form form = null; private Command exitCommand = null; private Command readCommand = null; private Command backCommand = null; public S40MIDletSMS2() { display = Display.getDisplay(this); form = new Form("RECEIVING MSG"); exitCommand = new Command("EXIT", Command.EXIT, 1); readCommand = new Command("Read", Command.SCREEN, 1); backCommand = new Command("BACK", Command.SCREEN, 2); form.append("SMS..."); form.addCommand(exitCommand); form.addCommand(readCommand); form.addCommand(backCommand); form.setCommandListener(this); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub closeConn(); } protected void pauseApp() { // TODO Auto-generated method stub closeConn(); } protected void startApp() throws MIDletStateChangeException { // TODO Auto-generated method stub display.setCurrent(form); setListener(); } private void closeConn() { if (mc != null) { try { mc.setMessageListener(null); mc.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private void setListener() { try { mc = (MessageConnection) Connector.open("sms://:5055"); mc.setMessageListener(this); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void readNewMessage() { new Thread() { public void run() { Message msg = null; try { msg = mc.receive(); } catch (InterruptedIOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if ((msg != null) && (msg instanceof TextMessage)) { TextMessage msgContent = (TextMessage) msg; form.append("Address:" + msgContent.getAddress().toString() + "\n"); form.append("Time:" + msgContent.getTimestamp().toString() + "\n"); form.append("Content:" + msgContent.getPayloadText()); } display.setCurrent(form); } }.start(); } public void commandAction(Command command, Displayable displayable) { // TODO Auto-generated method stub if (command == readCommand) { readNewMessage(); } else if (command == backCommand) { display.setCurrent(form); } else if (command == exitCommand) { notifyDestroyed(); } } public void notifyIncomingMessage(MessageConnection mc) { // TODO Auto-generated method stub if (this.mc == mc) { Alert alert = new Alert("New Message", "Do you want read this message?", null, AlertType.CONFIRMATION); alert.setTimeout(Alert.FOREVER); alert.addCommand(readCommand); alert.addCommand(backCommand); alert.setCommandListener(this); display.setCurrent(alert, form); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值