Java me 如何实现在SMS发送到端口50000时调用的MIDlet…代码不起作用

Java me 如何实现在SMS发送到端口50000时调用的MIDlet…代码不起作用,java-me,sms,midp,Java Me,Sms,Midp,如何实现在SMS发送到端口50000时调用的MIDlet 代码不起作用。手机无法接收短信,短信通过模拟器(JavaMeSDK)发送 要接收SMS,应进行哪些设置 我的代码: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.IOException; import javax.microedition.io.PushR

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

代码不起作用。手机无法接收短信,短信通过模拟器(JavaMeSDK)发送

要接收SMS,应进行哪些设置

我的代码:

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

我从网上找到了这段代码,它非常有效。你可以自己试试

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

}

我不确定我是否完全理解这个问题。但你需要了解

所以有两种类型的推注册,静态和动态

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

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

最后,你说

sms通过仿真器发送


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

您知道JSR 120和JSR 205 API()用于此目的吗?您发布的代码片段没有使用这些,为什么?@gnat所做的只是动态注册推送连接。IIRC这不需要使用WMA API…@funkybro我明白了。这使问题变得有趣。我不记得midlet在没有WMA的情况下以这种方式获得推送通知,也不知道这是否可能