如何在接收来自特定PORT号码的短信时自动调用/唤醒我的应用程序?如何在我的设备上查看此任何演示PORT编号可用于尝试此操作?
答案 0 :(得分: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();
}
}