001.
import
java.util.*;
002.
import
java.io.IOException;
003.
import
javax.microedition.io.*;
004.
import
javax.microedition.lcdui.*;
005.
import
javax.wireless.messaging.*;
006.
import
javax.microedition.midlet.*;
007.
008.
009.
010.
011.
012.
public
class
smsKu
extends
MIDlet
implements
CommandListener, Runnable,
013.
MessageListener {
014.
015.
Display display;
016.
Form form;
017.
List menuList;
018.
Command exitCom, replyCom, backCom, clrCom, sendCom;
019.
TextBox textSmsMasuk, textSmsKeluar;
020.
TextField textNo;
021.
String port, isiSmsKeluar;
022.
Thread thread;
023.
String[] connections;
024.
boolean
done;
025.
MessageConnection smsconn;
026.
Message msg;
027.
028.
public
smsKu() {
029.
display = Display.getDisplay(
this
);
030.
form =
new
Form(
"sms-an Yuk"
);
031.
exitCom =
new
Command(
"keluar"
, Command.EXIT,
1
);
032.
replyCom =
new
Command(
"balas"
, Command.SCREEN,
1
);
033.
backCom =
new
Command(
"Kembali"
, Command.BACK,
3
);
034.
clrCom =
new
Command(
"Hapus Tulisan"
, Command.OK,
2
);
035.
sendCom =
new
Command(
"Kirim"
, Command.OK,
1
);
036.
menuList =
new
List(
"Menu sms Ku"
, List.IMPLICIT);
037.
port =
"4321"
;
038.
textSmsMasuk =
new
TextBox(
null
,
null
,
500
, TextField.ANY);
039.
textSmsKeluar =
new
TextBox(
"Silakan Menulis Sms"
,
null
,
040.
500
, TextField.ANY);
041.
textNo =
new
TextField(
"Masukan Nomor Tujuan"
,
""
,
042.
15
, TextField.PHONENUMBER);
043.
}
044.
045.
public
void
startApp() {
046.
bukaKoneksi();
047.
menu();
048.
}
049.
050.
public
void
pauseApp() {
051.
}
052.
053.
public
void
destroyApp(
boolean
unconditional) {
054.
055.
056.
done =
true
;
057.
thread =
null
;
058.
if
(smsconn !=
null
) {
059.
try
{
060.
smsconn.close();
061.
}
catch
(IOException e) {
062.
}
063.
}
064.
065.
066.
notifyDestroyed();
067.
}
068.
069.
public
void
menu() {
070.
menuList.append(
"Tulis Sms"
,
null
);
071.
menuList.append(
"about"
,
null
);
072.
menuList.append(
"Keluar"
,
null
);
073.
menuList.addCommand(exitCom);
074.
menuList.setCommandListener(
this
);
075.
display.setCurrent(menuList);
076.
}
077.
078.
public
void
tulisSms() {
079.
textSmsKeluar.addCommand(sendCom);
080.
textSmsKeluar.addCommand(clrCom);
081.
textSmsKeluar.addCommand(backCom);
082.
textSmsKeluar.setCommandListener(
this
);
083.
display.setCurrent(textSmsKeluar);
084.
}
085.
086.
public
void
masukNo() {
087.
form.setTitle(
"Sms Siap dikirim"
);
088.
form.append(textNo);
089.
form.addCommand(backCom);
090.
form.addCommand(sendCom);
091.
form.setCommandListener(
this
);
092.
display.setCurrent(form);
093.
}
094.
095.
public
void
kirim() {
096.
new
Thread(
new
Runnable() {
097.
098.
public
void
run() {
099.
try
{
100.
String isiSms = isiSmsKeluar;
101.
String noTujuan = textNo.getString();
102.
String address =
"sms://"
+ noTujuan +
":"
+ port;
103.
MessageConnection smsConnKir =
null
;
104.
Message isiSmsNya =
null
;
105.
smsConnKir = (MessageConnection) Connector.open(address);
106.
TextMessage txtmessage =
107.
(TextMessage) smsConnKir.newMessage(MessageConnection.TEXT_MESSAGE);
108.
txtmessage.setAddress(address);
109.
txtmessage.setPayloadText(isiSms);
110.
isiSmsNya = txtmessage;
111.
smsConnKir.send(isiSmsNya);
112.
113.
}
catch
(Throwable t) {
114.
t.printStackTrace();
115.
}
116.
}
117.
}).start();
118.
}
119.
120.
public
void
terima(String isi, String no, Date tgl) {
121.
String nonya = fixNomer(no);
122.
textSmsMasuk =
new
TextBox(
"Sms dari:"
+ nonya +
" "
+ tgl, isi,
123.
isi.length(), TextField.ANY);
124.
textSmsMasuk.addCommand(replyCom);
125.
textSmsMasuk.addCommand(exitCom);
126.
textSmsMasuk.setCommandListener(
this
);
127.
display.setCurrent(textSmsMasuk);
128.
}
129.
130.
public
void
bukaKoneksi() {
131.
String smsConnection =
"sms://:"
+ port;
132.
done =
true
;
133.
thread =
null
;
134.
135.
try
{
136.
smsconn = (MessageConnection) Connector.open(smsConnection);
137.
smsconn.setMessageListener(
this
);
138.
}
catch
(IOException ioe) {
139.
ioe.printStackTrace();
140.
}
141.
142.
connections = PushRegistry.listConnections(
true
);
143.
144.
done =
false
;
145.
thread =
new
Thread(
this
);
146.
thread.start();
147.
}
148.
149.
public
void
tutupKoneksi() {
150.
try
{
151.
smsconn.close();
152.
}
catch
(IOException e) {
153.
}
154.
}
155.
156.
public
String fixNomer(String no) {
157.
158.
159.
String hs =
null
;
160.
hs = no.substring(
6
, no.length());
161.
return
hs;
162.
}
163.
164.
public
void
commandAction(Command c, Displayable d) {
165.
if
(c == exitCom) {
166.
destroyApp(
true
);
167.
}
168.
if
(d == menuList) {
169.
if
(menuList.isSelected(
0
)) {
170.
tulisSms();
171.
}
else
if
(menuList.isSelected(
1
)) {
172.
Alert about =
new
Alert(
"About"
,
"Aplikasi SMS buatan sendiri"
,
173.
null
, AlertType.INFO);
174.
display.setCurrent(about);
175.
}
else
if
(menuList.isSelected(
2
)) {
176.
destroyApp(
true
);
177.
}
178.
}
179.
if
(d == textSmsKeluar) {
180.
if
(c == sendCom) {
181.
form.deleteAll();
182.
masukNo();
183.
}
else
if
(c == clrCom) {
184.
textSmsKeluar.setString(
""
);
185.
}
else
if
(c == backCom) {
186.
display.setCurrent(menuList);
187.
}
188.
}
189.
if
(d == textSmsMasuk) {
190.
if
(c == exitCom) {
191.
display.setCurrent(menuList);
192.
}
else
if
(c == replyCom) {
193.
tulisSms();
194.
}
195.
}
196.
if
(d == form) {
197.
if
(c == backCom) {
198.
form.deleteAll();
199.
display.setCurrent(textSmsKeluar);
200.
}
else
if
(c == sendCom) {
201.
isiSmsKeluar = textSmsKeluar.getString();
202.
textSmsKeluar.setString(
""
);
203.
kirim();
204.
}
205.
}
206.
}
207.
208.
public
void
run() {
209.
try
{
210.
msg = smsconn.receive();
211.
String senderAddress = msg.getAddress();
212.
Date waktuSms = msg.getTimestamp();
213.
if
(msg
instanceof
TextMessage) {
214.
String isiSmsMasuk = ((TextMessage) msg).getPayloadText();
215.
terima(isiSmsMasuk, senderAddress, waktuSms);
216.
}
217.
tutupKoneksi();
218.
bukaKoneksi();
219.
}
catch
(IOException e) {
220.
}
221.
}
222.
223.
public
void
notifyIncomingMessage(MessageConnection conn) {
224.
if
(thread ==
null
) {
225.
done =
false
;
226.
thread =
new
Thread(
this
);
227.
thread.start();
228.
}
229.
}
230.
}