我如何发送多条短信'在一个程序?

时间:2013-03-02 12:33:26

标签: java smslib

我想通过使用smslib发送多个短信'。我创建了一个Java类来循环发送SMS。但它只工作一次。然后它返回以下异常:

org.smslib.GatewayException: Comm library exception:
   java.lang.RuntimeException: javax.comm.PortInUseException:
      Port currently owned by org.smslib
at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:102)
at org.smslib.modem.AModemDriver.connect(AModemDriver.java:114)
at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:189)
at org.smslib.Service$1Starter.run(Service.java:276)

这是我的班级:

import javax.swing.JOptionPane;
import org.smslib.AGateway;
import org.smslib.IOutboundMessageNotification;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;

public class SendSMS1 {

private static String id;
private static String port;
private static int bitRate;
private static String modemName;
private static String modemPin;
private static String SMSC;

public static void doIt(String number, String text) {
    try {
        OutboundMessage msg;
        OutboundNotification outboundNotification = new OutboundNotification();
        SerialModemGateway gateway = new SerialModemGateway(id, port, bitRate,     modemName, "E17u-1");
        gateway.setInbound(true);
        gateway.setOutbound(true);
        gateway.setSimPin(modemPin);
        Service.getInstance().setOutboundMessageNotification(outboundNotification);
        Service.getInstance().addGateway(gateway);
        Service.getInstance().startService();
        msg = new OutboundMessage(number, text);
        Service.getInstance().sendMessage(msg);
        System.out.println(msg);
        Service.getInstance().stopService();
    } catch (Exception ex) {
        if (ex.getStackTrace()[2].getLineNumber() == 189) {
            JOptionPane.showMessageDialog(null,
                    "Port currently owned by usb Modem's application. \n Please close it & run the programm again.",
                    "Port Exception",
                    JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        } else {
            JOptionPane.showMessageDialog(null,
                    ex.getMessage(),
                    "Sending faile",
                    JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
    }
}

public static class OutboundNotification implements IOutboundMessageNotification {
    public void process(AGateway gateway, OutboundMessage msg) {
        System.out.println("Outbound handler called from Gateway: " + gateway.getGatewayId());
        System.out.println(msg);
    }
}

public static void main(String[] args) throws Exception {
    String number = "+94772347634", text = "Hello world";
    modemName = "Huwawi";
    port = "COM4";
    bitRate = 115200;
    modemPin = "0000";
    SMSC = "+947500010";
    for (int i = 0; i < 5; i++) {
    SendSMS1 app = new SendSMS1();
    app.doIt(number, text);
    }
  }
}

请给我建议,我该怎么办?

3 个答案:

答案 0 :(得分:1)

您必须删除网关。 可以用这个 -   Service.getInstance()removeGateway(网关); 你应该在doIt方法中插入它 - Service.getInstance()。stopService();

答案 1 :(得分:0)

我不知道smslib,但是我想象一切,直到startService应该只发生一次,而不是每次你想发送消息,如下所示:

class SendSMS1
{
    public SendSMS1(String modemName, String port, int bitRate,
                    String modemPin, String SMSC)
    {
        OutboundNotification outboundNotification = new OutboundNotification();
        SerialModemGateway gateway = new SerialModemGateway(id, port, bitRate,     modemName, "E17u-1");
        gateway.setInbound(true);
        gateway.setOutbound(true);
        gateway.setSimPin(modemPin);
        Service.getInstance().setOutboundMessageNotification(outboundNotification);
        Service.getInstance().addGateway(gateway);
        Service.getInstance().startService();
    }

    public static void doIt(String number, String text) {
        try {
            OutboundMessage msg = new OutboundMessage(number, text);
            Service.getInstance().sendMessage(msg);
            System.out.println(msg);
            Service.getInstance().stopService();
        } catch (Exception ex) {
            if (ex.getStackTrace()[2].getLineNumber() == 189) {
                JOptionPane.showMessageDialog(null,
                        "Port currently owned by usb Modem's application. \n Please close it & run the programm again.",
                        "Port Exception",
                        JOptionPane.ERROR_MESSAGE);
                ex.printStackTrace();
            } else {
                JOptionPane.showMessageDialog(null,
                        ex.getMessage(),
                        "Sending faile",
                        JOptionPane.ERROR_MESSAGE);
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        String number = "+94772347634", text = "Hello world";
        SendSMS1 app = new SendSMS1("Huwawi", "COM4", 115200, "0000", "+947500010");
        for (int i = 0; i < 5; i++) {
            app.doIt(number, text);
        }
    }
}

答案 2 :(得分:0)

改变这个。它正在运作

public static void main(String[] args) throws Exception {
        Calendar now = Calendar.getInstance();
            String ar=(dateFormat.format(now.getTime()));
        String text = "We are the Future of Science "+" "+ar;

        SendSMS app = new SendSMS("Huawei", "COM20", 115200, "0000", "+9477000003");
        for (int i = 0; i < 1; i++) {

            String[] numbers = {"+num","+num","+num","+num","+num","+num","+num","+num"};
      for (String item : numbers) {

            Service.getInstance().startService();
            app.doIt(item, text);


            Service.getInstance().stopService();
        }}
    }