我正在j2me中开发一个与Wireless Messaging API(WMA)配合使用的应用程序。应用程序的任务是在2手机之间发送和接收短信。当我在NetBeans模拟器中运行应用程序时,它工作正常,但当我在诺基亚5200手机上运行它时,我在手机上给出了这个例外:
Security java/lang/SecurituException Not allowed to open connection
我的应用的JAD文件是:
MIDlet-1: Midlet, , hello.Midlet
MIDlet-Jar-Size: 36375
MIDlet-Jar-URL: SinaNetwork.jar
MIDlet-Name: SinaNetwork
MIDlet-Permissions: javax.microedition.io.Connector.sms, javax.wireless.messaging.sms.receive, javax.wireless.messaging.sms.send
MIDlet-Vendor: Vendor
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-2.0
应用程序代码也是:
Thread receive=new Thread(new Runnable() {
MessageConnection ms;
public void run() {
// System.out.print("*****************SALAM*******");
try {
ms= (MessageConnection) Connector.open("sms://:"+7000);
// System.out.println("Zoor mizanam Receive konam");
Date zaman=new Date();
long zamanTemp=zaman.getTime();
TextMessage tempmes=(TextMessage) ms.receive();
// System.out.print("SMS receive: "+tempmes.getPayloadText());
Midlet.messageReceived=true;
Midlet.ReceivedThatWeDontKnowIsRandom=tempmes.getPayloadText();
}
catch(SecurityException eds)
{
eds.printStackTrace();
// System.out.print("");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
});
receive.start();
Thread sendmesThread=new Thread(new Runnable() {
public void run() {
try {
MessageConnection mc=(MessageConnection) Connector.open("sms://"+Midlet.smsPhoneNumber+":"+5000);
TextMessage tm=(TextMessage) mc.newMessage(MessageConnection.TEXT_MESSAGE);
// System.out.print("PNF : "+phoneNumberField.getString());
tm.setPayloadText(rd+"-"+phoneNumberField.getString());
mc.send(tm);
// System.out.print("message sent from client on port 5000");
mc.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
});
sendmesThread.start();
我认为问题是应用程序无法打开发送或接收短信的连接,但我不知道为什么因为我在模拟器中没有问题。
答案 0 :(得分:1)
您的问题可能存在于您尝试使用的端口上:7000和5000.我相信您希望SMS跳过用户收件箱并仅由应用程序处理,对吧?
在此之前,请确保您的应用可以使用http://www.developer.nokia.com/Community/Wiki/How_to_send_text_SMS_in_Java_ME下面的代码发送简单的短信
public boolean sendSms(String number, String message){
boolean result = true;
try {
//sets address to send message
String addr = "sms://"+number;
// opens connection
MessageConnection conn = (MessageConnection) Connector.open(addr);
// prepares text message
TextMessage msg =
(TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
//set text
msg.setPayloadText(message);
// send message
conn.send(msg);
conn.close();
} catch (SecurityException se) {
// probably the user has not allowed to send sms
// you may want to handle this differently
result = false;
} catch (Exception e) {
result = false;
}
return result;
}
评论后更新
关于安全例外:也许手机只接受制造商和/或电信运营商/运营商的签名。在不同的手机上试用这个签名版本。