Writing MIDlet for SMS - IV
11 April 2008I will present the run method in this post.
Do remember that we are writing a multi threaded SMS MIDLet. The MIDLet signature reads like:
public class SMSReceive extends MIDlet implements CommandListener, Runnable, MessageListener { ... }
Lets define the run method:
public void run() { try { msg = smsconn.receive(); if (msg != null) { senderAddress = msg.getAddress(); content.setTitle("From: " + senderAddress); if (msg instanceof TextMessage) { content.setString(((TextMessage)msg).getPayloadText()); } else { StringBuffer buf = new StringBuffer(); byte[] data = ((BinaryMessage)msg).getPayloadData(); for (int i = 0; i < data.length; i++) { int intData = (int)data & 0xFF; if (intData < 0x10) { buf.append("0"); } buf.append(Integer.toHexString(intData)); buf.append(' '); } content.setString(buf.toString()); } content.addCommand(replyCommand); display.setCurrent(content); } } catch (IOException e) { e.printStackTrace(); } }
The method is used to read the SMS message. If the message is a text message then things are simple. In the other case we have to read the message in a byte array.
Related Posts:
- Writing MIDlet for SMS - VI
- Writing MIDlet for SMS - I
- Deploying MIDlets onto Mobile Devices (III)
- Writing MIDlet for SMS - II
- MIDlet Suite
- Writing MIDlet for SMS - III
- Writing MIDlet for SMS - V
- Creating J2ME MIDlet Project - I
- Record Management System (III)
- Converting an existing project to an EclipseME project
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Writing MIDlet for SMS - IV