WMA (Wireless Messaging API) 是在MIDP2.0里边定义的无线消息API, 它们主要是用来接收和发送基于文本、二进制和MultiPart格式的消息。

/* 建立一个连接*/
public boolean connectSMSServer() {
    try {
        messageConnection messageConnection =
                (MessageConnection) Connector.open("sms://:" + port);
        messageConnection.setMessageListener(this);
    } catch (Exception e) {
    }
}

/* 发送文本短消息 */
public void sendTextmessage(String address, String message) {
    try {
        //生成一个新的短消息
        TextMessage textMessage = (TextMessage) messageConnection.newMessage(
                MessageConnection.TEXT_MESSAGE, address);
        textMessage.setPayloadText(message);
        messageConnection.send(textMessage);
    } catch (Exception e) {
    }
}

/* 接受文本短消息 */
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) {
    }
}

/* 收到消息通知 */
public synchronized void notifyIncomingMessage(MessageConnection conn) {
    //notiy thread of incoming message
    synchronized (this) {
        notify();
    }
}

/* 关闭连接*/
public void closeConnection() {
    if (messageConnection != null) {
        try {
            messageConnection.setMessageListener(null);
            messageConnection.close();
        } catch (Exception e) {
        }
    }
}