2011-11-15 68 views

回答

4

WMA(無線消息API)是MIDP 2.0中定義的無線消息API。這些API旨在處理文本,二進制和多部分消息。爲了建立連接,應用程序通過提供標識地址的URL連接字符串來獲取實現來自Connector類的MessageConnection的對象。

/* Make a connection */ 
public boolean connectSMSServer() 
{ 
    try 
    { 
    messageConnection messageConnection = 
     (MessageConnection)Connector.open("sms://:" + port); 
    messageConnection.setMessageListener(this); 
    } 
    catch (Exception e) { 
    } 
} 

/* Send text message */ 
public void sendTextmessage(String address,String message) 
{ 
    try 
    { 
    //creates a new TextMessage 
    TextMessage textMessage = (TextMessage)messageConnection.newMessage(
      MessageConnection.TEXT_MESSAGE, address); 
    textMessage.setPayloadText(message); 
    messageConnection.send(textMessage); 
    } 
    catch (Exception e) { 
    } 
} 

/* Recieve text message */ 
public void receiveTextMessage() 
{ 
    try 
    { 
    Message message = messageConnection.receive(); 
    if (message instanceof TextMessage) 
    { 
     TextMessage textMessage = (TextMessage)message; 
    } 
    else 
    { 
     //Message can be binary or multipart 
    }     
    } 
    catch (Exception e) { 
    } 
} 

/* Notify Incoming Message */ 
public synchronized void notifyIncomingMessage(MessageConnection conn) 
{ 
    //notiy thread of incoming message 
    synchronized (this) 
    { 
    notify(); 
    } 
} 

/* Close Connection */ 
public void closeConnection() 
{ 
    if (messageConnection != null) 
    { 
    try 
    { 
     messageConnection.setMessageListener(null); 
     messageConnection.close(); 
    } 
    catch (Exception e) { 
    } 
    } 
    } 
} 

當您編碼接收SMS時,您需要監聽一個特定的端口。 J2ME應用程序無法直接從收件箱中讀取。

相關問題