我正在开发一个用于发送和接收短信的网络bean的移动应用程序,我已完成发送文本,但我不知道如何在NetBeans移动应用程序中接收SMS?
答案 0 :(得分:3)
WMA(无线消息传递API)是MIDP 2.0中定义的无线消息传递API。这些apis旨在处理文本,二进制和多部分消息。要建立连接,应用程序通过提供标识地址的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) {
}
}
}
}
编码接收短信时,您需要收听一个特定端口。 J2ME应用程序无法直接从收件箱中读取。