J2ME SMS IOException

发布于 2024-08-20 03:34:29 字数 1791 浏览 2 评论 0原文

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

由于未知原因而无法发送”。 -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");
    }
}

I am trying to send a text message to a phone and I get an error

Fail to send because of unknown reason. -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");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦行七里 2024-08-27 03:34:29

你给你的jad添加权限了吗?

MIDlet 权限:javax.microedition.io.Connector.sms、javax.wireless.messaging.sms.send

Did you add permissions to your jad?

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

北斗星光 2024-08-27 03:34:29

各种原因:

  • PAYG 电话没有信用
  • 没有移动接收
  • SMS API 被手机运营商阻止
  • 用户拒绝安全提示(这将导致 SecurityException
  • 无效的手机号码

All sorts of reasons:

  • No credit on PAYG phone
  • No mobile reception
  • SMS API blocked by handset operator
  • User rejected security prompt (this would cause a SecurityException)
  • Invalid mobile number
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文