4

如何实现当 SMS 发送到端口 50000 时调用的 MIDlet?

代码不起作用。手机收不到短信,通过模拟器(JAVA Me SDK)发送短信。

接收短信需要做哪些设置?

我的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import javax.microedition.io.PushRegistry;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

/**
 * @author bonni
 */
public class Midletsms extends MIDlet implements CommandListener{

    protected Display display;                                  
     //boolean started=false;
      Form form = new Form("Welcome");
      Command mCommandQuit;

    public void startApp() {

        String url = "sms://:50000";
        try {

            PushRegistry.registerConnection(url,this.getClass().getName(), "*");
           // PushRegistry.registerConnection(url,"Midletsms.class", "*");
        } catch (IOException ex) {

        } catch (ClassNotFoundException ex) {

        }
         form.append("This midlet gets invoked when message is sent to port:50000");



             display = Display.getDisplay(this);
             display.setCurrent(form);

                   mCommandQuit = new Command("Quit", Command.EXIT, 0);
             form.addCommand(mCommandQuit);
             form.setCommandListener(this);


    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
     public void commandAction(Command c, Displayable d) {
       // throw new UnsupportedOperationException("Not supported yet.");
         String label = c.getLabel();
        if(label.equals("Quit"))
                {
                        destroyApp(false);
            notifyDestroyed();
                }
     }
}
4

2 回答 2

2

不确定我是否完全理解这个问题。但是您需要阅读有关PushRegistry的信息。

所以推送注册有静态和动态两种。

您给出的代码示例使用动态注册。 您将需要手动调用此 MIDlet 至少一次,以便进行推送注册。(旁白:在您的示例中,您在startApp方法中执行此操作,这是一个非常糟糕的主意!推送注册是一个潜在的阻塞操作,因此不应在诸如 的生命周期方法中完成startApp。您应该在新线程中执行此操作)。

另一种方法是静态注册,您将推送信息包含在jad. 推送端口将在安装 MIDlet 时注册,无需运行它。

最后你说

短信通过模拟器发送

这是什么意思?为了启动应用程序,您需要从另一个 MIDlet 在相关端口号上发送 SMS(如果您愿意,这可以在同一部手机上)。

于 2012-09-13T09:19:36.373 回答
0

我在Jimmy 的博客上找到了这段代码,它运行良好。你可以自己试试

SMSSender.java

public class SMSSender extends MIDlet implements CommandListener {

    private Form formSender = new Form("SMS Sender");
    private TextField tfDestination = new TextField("Destination", "", 20, TextField.PHONENUMBER);
    private TextField tfPort = new TextField("Port", "50000", 6, TextField.NUMERIC);
    private TextField tfMessage = new TextField("Message", "message", 150, TextField.ANY);
    private Command cmdSend = new Command("Send", Command.OK, 1);
    private Command cmdExit = new Command("Exit", Command.EXIT, 1);
    private Display display;

    public SMSSender() {
        formSender.append(tfDestination);
        formSender.append(tfPort);
        formSender.append(tfMessage);
        formSender.addCommand(cmdSend);
        formSender.addCommand(cmdExit);
        formSender.setCommandListener(this);

        display = Display.getDisplay(this);
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

    protected void pauseApp() {

    }

    protected void startApp() throws MIDletStateChangeException {
        display.setCurrent(formSender);
    }

    public void commandAction(Command c, Displayable d) {
        if (c==cmdSend) {
            SendMessage.execute(tfDestination.getString(), tfPort.getString(), tfMessage.getString());
        } else if (c==cmdExit) {
            notifyDestroyed();
        }
    }

}



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();
    }

}

SMSReceiver.java

public class SMSReceiver extends MIDlet implements CommandListener, MessageListener {

    private Form formReceiver = new Form("SMS Receiver");
    private TextField tfPort = new TextField("Port", "50000", 6, TextField.NUMERIC);
    private Command cmdListen = new Command("Listen", Command.OK, 1);
    private Command cmdExit = new Command("Exit", Command.EXIT, 1);
    private Display display;

    public SMSReceiver() {
        formReceiver.append(tfPort);
        formReceiver.addCommand(cmdListen);
        formReceiver.addCommand(cmdExit);
        formReceiver.setCommandListener(this);

        display = Display.getDisplay(this);
    }

    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException {

    }

    protected void pauseApp() {

    }

    protected void startApp() throws MIDletStateChangeException {
        display.setCurrent(formReceiver);
    }

    public void commandAction(Command c, Displayable d) {
        if (c==cmdListen) {
            ListenSMS sms = new ListenSMS(tfPort.getString(), this);
            sms.start();
            formReceiver.removeCommand(cmdListen);
        } else if (c==cmdExit) {
            notifyDestroyed();
        }
    }

    public void notifyIncomingMessage(MessageConnection conn) {
        Message message;
        try {
            message = conn.receive();
            if (message instanceof TextMessage) {
                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();
        }
    }

}

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();
        }
    }

}
于 2012-09-13T02:11:29.747 回答