import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.*;
import java.util.Vector;
public class KirimMMS extends MIDlet
implements CommandListener {
private Display display;
private Form form;
private TextField destination;
private Image image = null;
private MMSThread thread;
private final String textMsg = "Contoh pesan teks";
private final String imageFile = "/icons/Duke.png";
private final Command cmdKeluar =
new Command("Keluar", Command.EXIT, 1);
private final Command cmdKirim =
new Command("Kirim", Command.SCREEN, 1);
public KirimMMS() {
display = Display.getDisplay(this);
form = new Form("Mengirim MMS");
destination = new TextField("Nomor tujuan:", null, 15,
TextField.PHONENUMBER);
form.append(destination);
//pesan teks
StringItem si = new StringItem("Pesan teks:",
"\n" + textMsg + "\n");
form.append(si);
//pesan gambar
try {
image = Image.createImage(imageFile);
} catch(IOException ioe) {
ioe.printStackTrace();
}
ImageItem ii = new ImageItem("Pesan gambar:", image,
Item.LAYOUT_LEFT, null);
form.append(ii);
form.addCommand(cmdKeluar);
form.addCommand(cmdKirim);
form.setCommandListener(this);
}
public void startApp() {
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == cmdKeluar) {
destroyApp(false);
notifyDestroyed();
} else {
thread = new MMSThread(this);
thread.start();
}
}
// inner class
class MMSThread extends Thread {
private KirimMMS midlet;
private Vector parts;
public MMSThread(KirimMMS midlet) {
this.midlet = midlet;
}
public void run() {
String appID = midlet.getAppProperty("MMS-ApplicationID");
String phoneNumber = destination.getString();
String address = "mms://" + phoneNumber + ":" + appID;
parts = new Vector();
// pesan teks
String encoding = "UTF-8";
try {
byte[] textcontents = textMsg.getBytes(encoding);
addPart(
new MessagePart(textcontents, 0, textcontents.length,
"text/plain", "id1",
"contentLocation", encoding));
} catch (Exception e) {
//
}
// pesan gambar
try {
InputStream is =
getClass().getResourceAsStream(imageFile);
byte[] imagecontents = new byte[is.available()];
is.read(imagecontents);
addPart(
new MessagePart(imagecontents, 0, imagecontents.length,
"image/png", "id2",
"Duke.png", null));
} catch (Exception e) {
//
}
MessageConnection mmsconn = null;
try {
mmsconn = (MessageConnection) Connector.open(address);
MultipartMessage mmmessage =(MultipartMessage)
mmsconn.newMessage(
MessageConnection.MULTIPART_MESSAGE);
mmmessage.setAddress(address);
MessagePart[] mparts = getParts();
for (int i = 0; i < mparts.length; i++) {
mmmessage.addMessagePart(mparts[i]);
}
mmmessage.setSubject("Contoh MMS");
mmsconn.send(mmmessage);
} catch (Exception e) {
System.out.println(e.toString());
}
}
public MessagePart[] getParts() {
MessagePart[] partsArray = new MessagePart[parts.size()];
parts.copyInto(partsArray);
return partsArray;
}
public void addPart(MessagePart part) {
parts.addElement(part);
}
}
}
No comments:
Post a Comment