import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.*;
public class TerimaMMS extends MIDlet
implements CommandListener, MessageListener, Runnable {
private Display display;
private Form content;
private Alert alert;
private MessageConnection mmsConn;
private Message message;
private Thread thread;
private String senderAddress, subject, connectionList[];
private boolean done;
private final Command cmdKeluar;
public TerimaMMS() {
display = Display.getDisplay(this);
cmdKeluar = new Command("Keluar", Command.EXIT, 1);
content = new Form("Menerima MMS");
content.addCommand(cmdKeluar);
content.setCommandListener(this);
String appID = getAppProperty("MMS-ApplicationID");
String address = "mms://:" + appID;
try {
mmsConn = (MessageConnection) Connector.open(address);
mmsConn.setMessageListener(this);
} catch (IOException ioe) {
ioe.printStackTrace();
}
connectionList = PushRegistry.listConnections(true);
if (connectionList == null || connectionList.length == 0) {
content.append("waiting for MMS on application ID: " +
appID);
}
done = false;
thread = new Thread(this);
thread.start();
}
public void startApp() {
display.setCurrent(content);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if (c == cmdKeluar) {
destroyApp(false);
notifyDestroyed();
}
}
public void notifyIncomingMessage(MessageConnection conn) {
if (thread == null && !done) {
thread = new Thread(this);
thread.start();
}
}
public void run() {
receiveMMS();
}
public void receiveMMS() {
try {
message = mmsConn.receive();
if (message != null) {
senderAddress = message.getAddress();
content.deleteAll();
String titleStr = senderAddress.substring(6);
int colonPos = titleStr.indexOf(":");
if (colonPos != -1) {
titleStr = titleStr.substring(0, colonPos);
}
content.setTitle("Dari: " + titleStr);
if (message instanceof MultipartMessage) {
MultipartMessage mpm = (MultipartMessage) message;
StringBuffer buff = new StringBuffer("Judul: ");
buff.append((subject = mpm.getSubject()));
buff.append("\nTanggal: ");
buff.append(mpm.getTimestamp().toString());
StringItem messageItem = new StringItem("Pesan",
buff.toString());
messageItem.setLayout(Item.LAYOUT_NEWLINE_AFTER);
content.append(messageItem);
MessagePart[] parts = mpm.getMessageParts();
if (parts != null) {
for (int i = 0; i < parts.length; i++) {
buff = new StringBuffer();
MessagePart mp = parts[i];
buff.append("Tipe isi: ");
String mimeType = mp.getMIMEType();
buff.append(mimeType);
String contentLocation = mp.getContentLocation();
buff.append("\nIsi:\n");
byte[] ba = mp.getContent();
if (mimeType.equals("image/png") ||
mimeType.equals("image/jpg")) {
content.append(buff.toString());
Image img = Image.createImage(ba, 0, ba.length);
ImageItem ii = new ImageItem(contentLocation,
img, Item.LAYOUT_NEWLINE_AFTER,
contentLocation);
content.append(ii);
} else {
buff.append(new String(ba));
StringItem si =
new StringItem("Bagian", buff.toString());
si.setLayout(Item.LAYOUT_NEWLINE_AFTER);
content.append(si);
}
}
}
}
display.setCurrent(content);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
No comments:
Post a Comment