J2ME SMS IOException

时间:2010-02-04 20:30:43

标签: java-me sms ioexception

我正在尝试向手机发送短信,但收到错误

由于未知原因未能发送。 -java.io.IOException

import javax.microedition.io.Connector;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

public class Midlet extends MIDlet {
    Form form = new Form("Form");
    Display display;
    public void startApp()
    {
        display = Display.getDisplay(this);
        display.setCurrent(form);

        sendSMS("Hello from j2me");
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    private void sendSMS(String s) {
        String destination = "+12234567890";
        String addr = "sms://" + destination;

        out("Setting up message");
        MessageConnection sender = null;

        try 
        {
            try 
            {
                    sender = (MessageConnection) Connector.open(addr);
                    TextMessage msg = (TextMessage) sender.newMessage(MessageConnection.TEXT_MESSAGE);
                    msg.setPayloadText(s);
                    out("sending");
                    sender.send(msg);
                    out("sent successfully");
            } 
            catch (Exception ex) 
            {
                    out("Error1:" + ex.getMessage() + " : " + ex.toString() + "\n\n");
            } 
            finally 
            {
                sender.close();
            }
        } 
        catch (Exception ex) {
                //handle exception
                out("Error2:" + ex.getMessage() + " : " + ex.toString() + "\n\n");
        }
    }

    private void out(String str)
    {
        form.append(str + "\n");
    }
}

2 个答案:

答案 0 :(得分:2)

您是否为jad添加了权限?

MIDlet-Permissions:javax.microedition.io.Connector.sms,javax.wireless.messaging.sms.send

答案 1 :(得分:0)

各种原因:

  • PAYG手机没有信用
  • 没有移动接待
  • 手机运营商阻止的短信API
  • 用户拒绝安全提示(这会导致SecurityException
  • 手机号码无效