Program Kirim SMS dan Terima SMS
KIRIMSMS
package mobileapplication2;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;
public class KirimSMS extends MIDlet implements CommandListener, Runnable {
private Display display;
private Command cmdKeluar, cmdLanjut, cmdKembali, cmdKirim;
private Displayable prevScreen;
private TextBox tfNum, tfText;
private String port;
private Thread thread;
private Alert alert;
private final String INVALID_PHONE =
"Nomor yang dimasukkan tidak absah";
public KirimSMS() {
display = Display.getDisplay(this);
port = getAppProperty("Port-SMS");
cmdKeluar = new Command("Keluar", Command.EXIT, 1);
cmdLanjut = new Command("Lanjut", Command.SCREEN, 1);
cmdKembali = new Command("Kembali", Command.BACK, 1);
cmdKirim = new Command("Kirim", Command.SCREEN, 1);
alert = new Alert(null);
alert.setTimeout(3000);
}
public void startApp() {
tfNum = generatePhoneInput();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c == cmdKeluar) {
destroyApp(false);
notifyDestroyed();
} else if (c == cmdKembali) {
display.setCurrent(tfNum);
} else if (c == cmdLanjut) {
if (!isValidPhoneNumber(tfNum.getString())) {
alert.setType(AlertType.ERROR);
alert.setString(INVALID_PHONE);
display.setCurrent(alert, tfNum);
} else {
tfText = generateMessageInput();
}
} else {
thread = new Thread((java.lang.Runnable) this);
thread.start();
}
}
public void run() {
MessageConnection conn = null;
String alamat = "sms://" + tfNum.getString() + ":" + port;
try {
conn = (MessageConnection) Connector.open(alamat);
TextMessage pesan = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
pesan.setAddress(alamat);
pesan.setPayloadText(tfText.getString());
conn.send(pesan);
alert.setString("Sedang mengirim ke nomoe" + alamat.substring(6));
alert.setType(AlertType.INFO);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private TextBox generatePhoneInput() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private boolean isValidPhoneNumber(String string) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private TextBox generateMessageInput() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
TERIMASMS
package mobileapplication2;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;
public class TerimaSMS extends MIDlet
implements CommandListener, Runnable, MessageListener {
private Display display;
private Alert alert;
private TextBox tfPesan;
private Thread thread;
private MessageConnection conn;
private Message pesan;
private String[] daftarKoneksi;
private boolean selesaiProses;
private String port;
private final Command cmdKeluar = new Command("Keluar", Command.EXIT, 1);
public TerimaSMS() {
port = getAppProperty("Port-SMS");
display = Display.getDisplay(this);
alert = new Alert("Terima SMS");
alert.setTimeout(Alert.FOREVER);
alert.addCommand(cmdKeluar);
alert.setCommandListener(this);
tfPesan = new TextBox(null, null, 500, TextField.ANY);
tfPesan.addCommand(cmdKeluar);
tfPesan.setCommandListener(this);
}
public void startApp() {
String alamatKoneksi = "sms://:" + port;
if (conn == null) {
try {
conn = (MessageConnection) Connector.open(alamatKoneksi);
conn.setMessageListener(this);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
daftarKoneksi = PushRegistry.listConnections(true);
if (daftarKoneksi == null || daftarKoneksi.length== 0) {
alert.setString("Menunggu SMS pada port" + port + "...");
}
startNewThread();
display.setCurrent(alert);
}
public void pauseApp() {
selesaiProses = true;
thread = null;
}
public void destroyApp(boolean unconditional) {
selesaiProses = true;
thread = null;
if (conn != null) {
try {
conn.close();
} catch (IOException e) {
}
}
}
public void commandAction(Command c, Displayable s) {
if (c == cmdKeluar || c == Alert.DISMISS_COMMAND) {
destroyApp(false);
notifyDestroyed();
}
}
// method ini ada karena kia mengimplementasikan
// interface Runnable
public void run() {
try {
pesan = conn.receive();
if (pesan != null) {
String alamatPengirim = pesan.getAddress();
tfPesan.setTitle("Pesan dari:" + alamatPengirim.substring(6));
if (pesan instanceof TextMessage) {
TextMessage pesanTeks = (TextMessage) pesan;
tfPesan.setString(pesanTeks.getPayloadText());
}
display.setCurrent(tfPesan);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
// method ini ada karena kita mengimplementasikan
// interface MessageListener
public void notifyIncomingMessage(MessageConnection conn) {
if (thread == null) {
startNewThread();
}
}
public void startNewThread() {
selesaiProses = false;
thread = new Thread(this);
thread.start();
}
}
Tidak ada komentar:
Posting Komentar