2013-03-02 26 views
0

我想通過使用smslib發送多個SMS。我讓一個Java類在一個循環中發送SMS。但它只有一次。然後返回了以下異常:如何在一個程序中發送多個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); 
    } 
    } 
} 

請給我建議,我應該怎麼做不同?

+0

我不知道smslib。但我認爲,您應該將實際發送的消息與服務分開。 – Quirin 2013-03-02 12:43:08

+0

Tnx 4回覆。我如何分開準備服務? – 2013-03-02 13:15:09

+0

像@Dukeling例子。 – Quirin 2013-03-02 13:23:18

回答

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); 
     } 
    } 
} 
+0

Tnx Dukeling。我改變我的班級爲例。現在它沒有任何例外。現在循環正常工作。它調用5次doIt方法。但問題是它只發送一條短信。 – 2013-03-03 08:50:47

0

變化this.It的工作

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(); 
     }} 
    } 
+0

請詳細解釋您在此代碼中所做的更改。 – ZeMoon 2014-06-10 09:22:39

1

您必須刪除網關。 可以使用這個 - Service.getInstance()。removeGateway(gateway); 你應該在doIt方法中插入它 - Service.getInstance()。stopService();

相關問題