How to implement a MIDlet that is called when sending SMS to port 50000 .... the code does not work

How to implement a MIDlet that is called when sending SMS to port 50,000?

The code does not work. SMS cannot be received on the phone, SMS is sent through the emulator (JAVA Me SDK).

What settings must be made to receive SMS?

my code is:

/* * 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
source share
2 answers

Not sure if I fully understand the problem. But you need to read PushRegistry .

Thus, there are two types of push logging, static and dynamic.

The code sample you provided uses dynamic registration. You will need to manually call this MIDlet at least once for the click registration to click . (Also: in your example, you do this in the startApp method, this is a very bad idea! Registering Push is a potentially blocking operation and therefore should not be done in a lifecycle method such as startApp . Do this in a new thread).

An alternative is static registration, in which you include push information in jad . The exact port will be registered when the MIDlet is installed, without having to launch it.

Finally you say

sms is sent through the emulator

what does it mean? To start the application you need to send an SMS message to the corresponding port number from another MIDlet (this can be on the same handset, if you want).

+2
source

I found this code online from the Jimmy blog , and it works great. You can try it on your own,

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

Source: https://habr.com/ru/post/1433871/


All Articles