Aplikasi Kirim dan Terima SMS Menggunakan Java




Aplikasi Kirim dan Terima SMS

Berikut ini adalah langkah-langkah untuk membuat program "Aplikasi Kirim dan Terima SMS" menggunakan software Sun Java(TM) Wireless Toolkit 2.5.2. 
  1. Pertama, jalankan Sun Java (TM) Wireless Toolkit 2.5.2. Buat 2 project yaitu kirimSMS dan terimaSMS. Setelah itu, klik New Project, di ProjectName kita ketikkan nama project1, yaitu kirimSMS, di MIDlet Class Name ketikkan tulisan yang sama, Lalu klik Create Project. Project2 diberi nama terimaSMS. Selanjutnya akan muncul kotak dialog setting, klik OK. 
  2. Buka TextPad, ketikan source code berikut ini:  
code kirimSMS:
import javax.microedition.io.Connector;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
public class kirimSMS extends MIDlet implements CommandListener,Runnable
{
private Form sendMessageForm;
private TextField number, msg;
private Command cmdOK,cmdExit;
private String telpNum, isiPesan = "";
public kirimSMS(){
cmdExit = new Command("Back", Command.EXIT, 2);
cmdOK = new Command("Send", Command.SCREEN, 2);
sendMessageForm = new Form("Send Message");
msg = new TextField("Message:", null, 50, TextField.ANY);
number = new TextField("Telepon:", null, 15, TextField.PHONENUMBER);
sendMessageForm.append(number);
sendMessageForm.append(msg);
sendMessageForm.addCommand(cmdOK);
sendMessageForm.addCommand(cmdExit);
sendMessageForm.setCommandListener(this);
}
protected void destroyApp(boolean arg0) {
notifyDestroyed();
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(sendMessageForm);
}
public void commandAction(Command arg0, Displayable arg1) {
if(arg0==cmdOK){
telpNum = number.getString();
isiPesan = msg.getString();
new Thread(this).start();
number.setString("");
msg.setString("");
Display.getDisplay(this).setCurrent(sendMessageForm);
}else if(arg0==cmdExit){

destroyApp(true);
}
}
public void run() {
try{
String nomorTelp = "sms://" + telpNum + ":1234";
MessageConnection conn =
(MessageConnection)Connector.open(nomorTelp);
TextMessage msg = (TextMessage)
conn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setPayloadText(isiPesan);
conn.send(msg);
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

code terimaSMS:
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
public class terimaSMS extends MIDlet implements Runnable,
CommandListener {
private TextBox tb;
private String text = "Menunggu pesan...";
private Command okComand;
public terimaSMS() {
tb = new TextBox("Hello MIDlet", text, 150, 0);
}
protected void destroyApp(boolean arg0) throws
MIDletStateChangeException {
}

protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(tb);
new Thread(this).start();
}
public void changeScreen() {
tb = new TextBox("SMS MIDlet", text, 150, 0);
Display.getDisplay(this).setCurrent(tb);
}
public void addConfirmation(String msg) {
tb = new TextBox("SMS MIDlet", text, 150, 0);
tb.setString(msg);
okComand = new Command("OK",Command.OK,1);
tb.addCommand(okComand);
tb.setCommandListener(this);
Display.getDisplay(this).setCurrent(tb);
}
public void run() {
try {
MessageConnection rconn = (MessageConnection) Connector
.open("sms://:1234");
while (true) {
Message msg = rconn.receive();
if (msg instanceof TextMessage) {
TextMessage tmsg = (TextMessage) msg;
String msgOut = tmsg.getPayloadText();
addConfirmation(msgOut);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void commandAction(Command arg0, Displayable arg1) {
}
}
  1. Simpan ke C:\Documents and Settings\users\j2mewtk\2.5.2\apps\kirimSMS\src (untuk nama users sesuaikan dengan nama user pada komputer anda). Dan project2 ke C:\Documents and Settings\Beand\j2mewtk\2.5.2\apps\terimaSMS\src. Setelah itu pada Sun Java (TM) Wireless Toolkit 2.5.2, pilih Open Project dan buka project AplikasiNilai, lalu Build dan Run. 
  2. Langkah selanjutnya adalah membuat Package. Untuk menjalankan aplikasi ini pada handphone, buat file .jad, file .jad ini nantinya akan kita pindahkan ke handphone dan kita install. Langkah-langkah pembuatan file .jad. 
    1. pilih Project
    2. pilih Package  
    3. pilih Create Package
    4. Maka file .jad akan tercipta, dan file tersebut disimpan di dalam direktori bin.
    5. Pindahkan file yang disimpan di dalam direktori bin ke handphone.




0 komentar:

Posting Komentar

 

Photo

Updates

Meet The Author