j2me 发送短信的问题!!!!
j2me发送短信的代码,在wtk中测试可以发送消息,但是放到手机上就发送不了,发送后手机有提示是否进行sms操作,.确定后就没反应了.这是什么问题啊???
package com.gao.sms;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;
import javax.microedition.io.Connector;
/**
* <p> Title: </p>
*
* <p> Description: </p>
*
* <p> Copyright: Copyright (c) 2007 </p>
*
* <p> Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class SMSSendFrame extends Form implements CommandListener {
private Display display;
public SMSSendFrame(Display display) {
super( "短信发送 ");
this.display = display;
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
// Set up this Displayable to listen to command events
setCommandListener(this);
// add the Exit command
addCommand(new Command( "Exit ", Command.EXIT, 1));
addCommand(sms_send);
this.append(sms_number);
this.append(sms_port);
this.append(sms_content);
sms_number.setLabel( "接收号码: ");
sms_number.setConstraints(TextField.PHONENUMBER);
sms_number.setMaxSize(20);
sms_port.setLabel( "接收端口 ");
sms_port.setConstraints(TextField.PHONENUMBER);
sms_port.setMaxSize(20);
sms_content.setLabel( "接收内容: ");
sms_content.setMaxSize(100);
sms_content.setPreferredSize(200, 132);
}
public void commandAction(Command command, Displayable displayable) {
/** @todo Add command handling code */
if (command.getCommandType() == Command.EXIT) {
// stop the MIDlet
SMSSendMIDlet.quitApp();
}
if (command == sms_send) {
send();
}
}
TextField sms_number = new TextField( " ", " ", 15, TextField.ANY);
TextField sms_port = new TextField( " ", " ", 15, TextField.ANY);
TextField sms_content = new TextField( " ", " ", 15, TextField.ANY);
private Command sms_send = new Command( "发送 ", Command.SCREEN, 1);
private void send() {
new Thread() {
public void run() {
String number = sms_number.getString();
if (number.equals( " ")) {
Alert alert = new Alert( "发送消息错误 ", "请输入接收的电话号码 ", null,
AlertType.ERROR);
alert.setTimeout(30000);
display.setCurrent(alert);
return;
}
//地址
String address;
if (number.startsWith( "+ ")) {
address = "sms:// " + number;
} else {
address = "sms://+ " + number;
}
//获得端口
String port = sms_port.getString();
if (!port.equals( " ")) {
address += ": " + port;
}
//System.out.println(address);
try {
MessageConnection mc = (MessageConnection) Connector.open(
address);
TextMessage msg = (TextMessage) mc.newMessage(
MessageConnection.TEXT_MESSAGE);
msg.setAddress(address);
msg.setPayloadText(sms_content.getString());
mc.send(msg);
Alert alert = new Alert( "发送消息 ", address, null,
AlertType.ERROR);
alert.setTimeout(10000);
display.setCurrent(alert);
mc.close();
mc = null;
} catch (Exception e) {
Alert alert = new Alert( "发送消息 ",e.toString(), null,
AlertType.ERROR);
alert.setTimeout(30000);
display.setCurrent(alert);
}
}
}.start();
}
}
[解决办法]
肯定是你代码的问题了,在发送的时候,端口用默认的就行了吧 String address = "sms:// " + number;
[解决办法]
给你个小demo,另外,不要用jb来写界面,没有任何好处。
sms_phone = xxxxxxx;
sms_body = xxxxxxxx;
if (null != sms_phone && null != sms_body) {
new Thread(this){
public void run() {
try {
String addr = new StringBuffer( "sms:// ").append(sms_phone).toString();
MessageConnection conn = null;
conn = (MessageConnection)Connector.open(addr);
TextMessage msg = (TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setPayloadText(sms_body);
conn.send(msg);
conn.close();
} catch(Exception e) {
//#ifdef nc_debug
e.printStackTrace();
//#endif
return;
}
}
}.start();
}
[解决办法]
如果要开发java程序就用moto的手机吧,它对jsr的支持力度是最大的,moto手机的第三方编程接口一般只有java,所以这方面应该比较强,不像nokia还有symbia的开放接口。
[解决办法]
有些手机虽然支持JSR75 JSR135等规范,但是由于自身厂商的安全性限制,根本就调用不了发短讯,文件操作等的底层功能.像MOTO和SE等的我就遇到很多这样的问题.
[解决办法]
短信发送机的实现
http://javaeye.5d6d.com/thread-7-1-1.html
[解决办法]
MessageConnection smsconn = null;
try {
/** Open the message connection. */
smsconn = (MessageConnection) Connector.open(“sms://”+[b]短信号码[/b]);
TextMessage txtmessage = (TextMessage) smsconn
.newMessage(MessageConnection.TEXT_MESSAGE);
txtmessage.setAddress(“sms://”+[b]短信号码[/b]);
txtmessage.setPayloadText( "just try ");
smsconn.send(txtmessage);
SmsSend = true;
gameState = Help;
} catch (Throwable t) {
System.out.println( "Send caught: ");
t.printStackTrace();
}
if (smsconn != null) {
try {
smsconn.close();
} catch (IOException ioe) {
System.out.println( "Closing connection caught: ");
ioe.printStackTrace();
}
}