2011-10-21 2 views
2

나는 SMS를 보내는 간단한 코드가 있습니다. 그것은 잘 작동합니다. 그냥 작은 문제. 어떻게하면 SMS를 보낼 수 없다는 것을 알 수 있습니까? 연결 또는 다른 방법으로 시간 제한이 있습니까? 네트워크가 없거나 SIM 카드가 없거나 신용이 없다고 가정 해 봅시다.블랙 베리 - SMS 시간 초과를 보내십시오

public static void sendSMS(String content, String number) { 
     MessageConnection mc = null; 
     TextMessage msg; 
     try { 

      mc = (MessageConnection) Connector.open("sms://" + number,Connector.WRITE,true); 
      msg = (TextMessage) mc.newMessage(MessageConnection.TEXT_MESSAGE); 
      msg.setPayloadText(content); 
      mc.send(msg); 

     } catch (final Exception e) { 
      e.printStackTrace(); 
      UiApplication.getUiApplication().invokeLater(new Runnable() { 

       public void run() { 
        Dialog.alert(e.getMessage()); 
       } 
      }); 

     } finally { 
      try { 
       if (mc != null) { 
        mc.close(); 
       } 
      } catch (IOException e) { 
      } 
     } 
    } 

답변

0
private void sendSMS(final String no, final String msg) { 

    try { 

     new Thread() { 

      public void run() { 

       if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) { 

        DatagramConnection dc = null; 

        try { 

         dc = (DatagramConnection) Connector.open("sms://" + no); 

         byte[] data = msg.getBytes(); 

         Datagram dg = dc.newDatagram(dc.getMaximumLength()); 

         dg.setData(data, 0, data.length); 

         dc.send(dg); 

         UiApplication.getUiApplication().invokeLater(new Runnable() { 

          public void run() { 

           try { 

            System.out.println("Message Sent Successfully : Datagram"); 

            Dialog.alert("Message Sent Successfully"); 

           } catch (Exception e) { 

            System.out.println("Exception **1 : " + e.toString()); 

            e.printStackTrace(); 

           } 

          } 

         }); 

        } catch (Exception e) { 

         System.out.println("Exception 1 : " + e.toString()); 

         e.printStackTrace(); 

        } finally { 

         try { 

          dc.close(); 

          dc = null; 

         } catch (IOException e) { 

          System.out.println("Exception 2 : " + e.toString()); 

          e.printStackTrace(); 

         } 

        } 

       } else { 

        MessageConnection conn = null; 

        try { 

         conn = (MessageConnection) Connector.open("sms://" + no); 

         //generate a new text message 

         TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE); 

         //set the message text and the address 

         tmsg.setAddress("sms://" + no); 

         tmsg.setPayloadText(msg); 

         //finally send our message 

         conn.send(tmsg); 

         UiApplication.getUiApplication().invokeLater(new Runnable() { 

          public void run() { 

           try { 

            System.out.println("Message Sent Successfully : TextMessage"); 

            Dialog.alert("Message Sent Successfully : TextMessage"); 

           } catch (Exception e) { 

            System.out.println("Exception **1 : " + e.toString()); 

            e.printStackTrace(); 

           } 

          } 

         }); 

        } catch (Exception e) { 

         System.out.println("Exception 3 : " + e.toString()); 

         e.printStackTrace(); 

        } finally { 

         try { 

          conn.close(); 

          conn = null; 

         } catch (IOException e) { 

          System.out.println("Exception 4 : " + e.toString()); 

          e.printStackTrace(); 

         } 

        } 

       } 

      } 

     }.start(); 

    } catch (Exception e) { 

     System.out.println("Exception 5 : " + e.toString()); 

     e.printStackTrace(); 

    } 
: 감사합니다 여기 코드입니다