2012-06-04 50 views
0

如何在從特定端口號接收短信時自動調用/喚醒我的應用程序?我怎樣才能在我的設備上檢查這個任何演示端口號碼是否可以試用?J2ME短信通知服務

+0

你試過了嗎? – Lucifer

+0

還沒有,因爲我沒有任何端口號來接收來自特定端口的消息。 – jayesh

+0

意味着??我沒有得到你 – Lucifer

回答

4

就學習貫徹下面的代碼段,

有關特定端口

public class SendMessage 
{ 
    public static void execute(final String destination, final String port, final String message) 
    { 
     Thread thread = new Thread(new Runnable() 
     { 
      public void run() 
      { 
       MessageConnection msgConnection; 
       try 
       { 
        msgConnection = (MessageConnection)Connector.open("sms://"+destination+":" + port); 
        TextMessage textMessage = (TextMessage)msgConnection.newMessage(
          MessageConnection.TEXT_MESSAGE); 
        textMessage.setPayloadText(message); 
        msgConnection.send(textMessage); 
        msgConnection.close(); 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     }); 

     thread.start(); 
    } 
} 

上發送短信有關特定端口讀短信

//線程聽消息

public class ListenSMS extends Thread 
{ 
    private MessageConnection msgConnection; 
    private MessageListener listener; 
    private String port; 

    public ListenSMS(String port, MessageListener listener) 
    { 
     this.port = port; 
     this.listener = listener; 
    } 

    public void run() 
    { 
     try 
     { 
      msgConnection = (MessageConnection)Connector.open("sms://:" + port); 
      msgConnection.setMessageListener(listener); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

//消息到達時

public void notifyIncomingMessage(MessageConnection conn) 
{ 
    Message message; 
    try 
    { 
     message = conn.receive(); 
     if (message instanceof TextMessage) 
     { 
        // here you invoke your application 
      TextMessage tMessage = (TextMessage)message; 
      formReceiver.append("Message received : "+tMessage.getPayloadText()+"\n"); 
     } 
     else 
     { 
      formReceiver.append("Unknown Message received\n"); 
     } 
    } 
    catch (InterruptedIOException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
}