4

SMS がポート 50000 に送信されたときに呼び出される MIDlet を実装する方法は?

コードが機能していません。SMS は電話では受信できません。SMS はエミュレータ (JAVA Me SDK) を介して送信されます。

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

2 に答える 2

2

問題を完全に理解しているかどうかわかりません。ただし、 PushRegistryについて読む必要があります。

したがって、プッシュ登録には静的と動的の 2 種類があります。

あなたが与えたコード例は、動的登録を使用しています。 プッシュ登録を行うには、この MIDlet を手動で少なくとも 1 回呼び出す必要があります。(余談: あなたの例では、startAppメソッドでこれを行っていますが、これは非常に悪い考えです! プッシュ登録は潜在的にブロックする操作であるため、 などのライフサイクル メソッドでstartAppは実行しないでください。新しいスレッドでこれを実行する必要があります。 )。

もう 1 つの方法は静的登録で、プッシュ情報を .xml ファイルに含めますjad。プッシュ ポートは、MIDlet のインストール時に登録されます。実行する必要はありません。

最後に、あなたは言います

SMSはエミュレーター経由で送信されます

これは何を意味するのでしょうか?アプリを開始するには、別の MIDlet から関連するポート番号で SMS を送信する必要があります (必要に応じて、同じハンドセットでもかまいません)。

于 2012-09-13T09:19:36.373 に答える
0

ジミーのブログからネットでこのコードを見つけましたが、完全に機能しています。ご自身でお試しいただけますので、

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 に答える