2015-06-14 41 views
-2

我有一些java代碼發送短信。

它會在15秒內多次返回一個送貨報告(但有些時候沒有發生)

我想停止那些短信發送功能,如果它沒有給出15秒的發貨報告。

如果你可以,請給出一個類似的示例代碼或

給一些鏈接,我應該閱讀這樣做。


非常感謝你......!退出函數如果不能在30秒內返回值java


`

public String sendTest(String port) 

{ 

modemName = "Huwawi"; 
    bitRate = 115200; 
    modemPin = "0000"; 
    SMSC = "+94785000008"; try { 
     OutboundMessage msg; 
     OutboundNotification outboundNotification = new OutboundNotification(); 
     SerialModemGateway gateway = new SerialModemGateway(id, "COM1", 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("+94710433607", "test successful !"); 

     Service.getInstance().sendMessage(msg); 

     OutboundMessage.MessageStatuses status = msg.getMessageStatus(); 


     System.out.println(msg.getMessageStatus()); 

     Service.getInstance().stopService(); 
     Service.getInstance().removeGateway(gateway); 

    } 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(); 
     } 
    }return port; 
} 

'
如果我給的代碼是從

Service.getInstance().startService(); 

卡住如果我給傳遞報告之前15秒獲得正確的端口號的錯誤的端口號。

如果我提供錯誤的端口號,代碼不會給出或返回任何值。 如果代碼在15秒內沒有返回值,我知道端口號是錯誤的。

所以我想幫助自動關閉功能,如果它不在15秒內返回一個值。

再次感謝...!

+2

所以ü[R要求我們做烏爾功課? – Bikku

+0

告訴你迄今爲止所嘗試過的。嘗試一下,如果你遇到麻煩,那麼有很多可以幫助你。但關鍵是你應該先嚐試。 – hagrawal

+0

它卡在哪裏?在Java代碼中,從描述中,我懷疑?在運行SQL時,那麼哪個數據庫?主機上的外部命令?沒有足夠的信息來回答。 – YoYo

回答

0

你應該製作一個線程,並在15秒內沒有任何事情發生時停止它。 我的意思是這樣:

Thread t = new Thread(){ 
    @Override 
    public void run() 
    { 
     // Send SMS 
    } 
} 
t.start(); 
// ... 
long time = System.currentTimeMillis(); 
while(true) 
{ 
    if(System.currentTimeMillis() - time > 15000) 
     t.stop(); 
} 

我希望你明白我說

+0

請注意'Thread.stop'已被棄用,因爲它本質上是不安全的:http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html如果您有可能,請修復阻止代碼或試圖找出是否有一種更安全的方法來解鎖它在具體情況下(例如,通過中斷它的'Thread.sleep'並修改一些屬性,使它不會回到睡眠狀態)。 – mastov