1

我正在 j2me 中开发一个与无线消息 API (WMA) 一起使用的应用程序。应用程序任务是在两部手机之间发送和接收短信。当我在 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();

我认为问题在于应用程序无法打开发送或接收短信的连接,但我不知道为什么,因为我在模拟器中没有问题。

4

1 回答 1

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;
}

评论后更新

关于安全例外:手机可能只接受制造商和/或电信运营商/运营商的签名。在不同的手机上试试这个签名版本。

于 2012-09-05T18:42:20.853 回答