.

Friday, June 19, 2009

SMS application with J2ME in handphone

Posted on/at 10:49 PM by Sagita

Which is the source code for fan applications in the phone, yesterday I have try .. which result lumayan ..

copy, save and run .. have fun ...

Ok. source code (along with the explanation, although not informative but oke) is:

plaincopy to view clipboardprint?


1.

2. import java.util.*;

3. import java.io.IOException;

4. import javax.microedition.io.*;

5. import javax.microedition.lcdui.*;

6. import javax.wireless.messaging.*;

7. import javax.microedition.midlet.*;

8.

9. /**

10. * @author MasWawa

11. * @see http://www.maswawa.web.id

12. */

13. public class smsKu extends MIDlet implements CommandListener, Runnable,

14. MessageListener {

15.

16. Display display;

17. Form form;

18. List menuList; //list untuk menu awal

19. Command exitCom, replyCom, backCom, clrCom, sendCom; //macem macem command yg digunakan

20. TextBox textSmsMasuk, textSmsKeluar; //tempat untuk text sms yg masuk dan yg mau dikirim

21. TextField textNo; //untuk tempat memasukan no tujuan

22. String port, isiSmsKeluar; //port dan isi sms

23. Thread thread;

24. String[] connections;

25. boolean done;

26. MessageConnection smsconn;

27. Message msg;

28.

29. public smsKu() {

30. display = Display.getDisplay(this);

31. form = new Form(“sms-an Yuk”);

32. exitCom = new Command(“keluar”, Command.EXIT, 1);

33. replyCom = new Command(“balas”, Command.SCREEN, 1);

34. backCom = new Command(“Kembali”, Command.BACK, 3);

35. clrCom = new Command(“Hapus Tulisan”, Command.OK, 2);

36. sendCom = new Command(“Kirim”, Command.OK, 1);

37. menuList = new List(“Menu sms Ku”, List.IMPLICIT);

38. port = “4321″; //port yang aku gunakan, silakan ganti sesuai keinginan masing masing

39. textSmsMasuk = new TextBox(null, null, 500, TextField.ANY);

40. textSmsKeluar = new TextBox(“Silakan Menulis Sms”, null,

41. 500, TextField.ANY);

42. textNo = new TextField(“Masukan Nomor Tujuan”, “”,

43. 15, TextField.PHONENUMBER);

44. }

45.

46. public void startApp() {

47. bukaKoneksi(); //disini membuka koneksi untuk siap menerima sms masuk

48. menu(); // bikin tampilan menu awal

49. }

50.

51. public void pauseApp() {

52. }

53.

54. public void destroyApp(boolean unconditional) {

55.

56. //sebelum aplikasinya keluar ato mati koneksi ditutup dulu

57. done = true;

58. thread = null;

59. if (smsconn != null) {

60. try {

61. smsconn.close();

62. } catch (IOException e) {

63. }

64. }

65.

66. //disini mati deh aplikasi kita

67. notifyDestroyed();

68. }

69.

70. public void menu() { //membuat menu awal untuk sementara ini baru ada 3 menu dulu

71. menuList.append(“Tulis Sms”, null);//1. menu tulis sms baru

72. menuList.append(“about”, null); //2. menu about (gak penting sih sebenarnya)

73. menuList.append(“Keluar”, null); //3. menu untuk keluar aplikasi

74. menuList.addCommand(exitCom); //add command keluar

75. menuList.setCommandListener(this);

76. display.setCurrent(menuList);

77. }

78.

79. public void tulisSms() { //membuat textbox untuk menulis sms baru

80. textSmsKeluar.addCommand(sendCom); //add command untuk kirim

81. textSmsKeluar.addCommand(clrCom); // add command untuk clear text

82. textSmsKeluar.addCommand(backCom); //add command untuk kembali kemenu awal

83. textSmsKeluar.setCommandListener(this);

84. display.setCurrent(textSmsKeluar);

85. }

86.

87. public void masukNo() { //disini untuk memasukan nomor tujuan yang mau dikirimi sms

88. form.setTitle(“Sms Siap dikirim”); //udah taukan yang ini?

89. form.append(textNo); //menambahkan textField untuk nomor tujuan

90. form.addCommand(backCom); //menambahkan command untuk kembali ke menu awal

91. form.addCommand(sendCom); //menambahkan command untuk mengirim

92. form.setCommandListener(this);

93. display.setCurrent(form);

94. }

95.

96. public void kirim() { //disini sms yang udah ditulis dikirim dengan perintah dibawah ini

97. new Thread(new Runnable() {

98. //perhatiin ya

99. public void run() {

100. try {

101. String isiSms = isiSmsKeluar;

102. String noTujuan = textNo.getString();

103. String address = “sms://” + noTujuan + “:” + port;

104. MessageConnection smsConnKir = null;

105. Message isiSmsNya = null;

106. smsConnKir = (MessageConnection) Connector.open(address);

107. TextMessage txtmessage =

108. (TextMessage) smsConnKir.newMessage(MessageConnection.TEXT_MESSAGE);

109. txtmessage.setAddress(address);

110. txtmessage.setPayloadText(isiSms);

111. isiSmsNya = txtmessage;

112. smsConnKir.send(isiSmsNya);

113.

114. } catch (Throwable t) {

115. t.printStackTrace();

116. }

117. }

118. }).start();

119. }

120.

121. public void terima(String isi, String no, Date tgl) { //ini untuk tempat membaca sms yang masuk

122. String nonya = fixNomer(no);

123. textSmsMasuk = new TextBox(“Sms dari:” + nonya + “ ” + tgl, isi,

124. isi.length(), TextField.ANY);

125. textSmsMasuk.addCommand(replyCom);

126. textSmsMasuk.addCommand(exitCom);

127. textSmsMasuk.setCommandListener(this);

128. display.setCurrent(textSmsMasuk);

129. }

130.

131. public void bukaKoneksi() { //membuka koneksi agar bisa menerima sms yg masuk

132. String smsConnection = “sms://:” + port;

133. done = true;

134. thread = null;

135.

136. try {

137. smsconn = (MessageConnection) Connector.open(smsConnection);

138. smsconn.setMessageListener(this);

139. } catch (IOException ioe) {

140. ioe.printStackTrace();

141. }

142.

143. connections = PushRegistry.listConnections(true);

144.

145. done = false;

146. thread = new Thread(this);

147. thread.start();

148. }

149.

150. public void tutupKoneksi() { //untuk menutup koneksi sms

151. try {

152. smsconn.close();

153. } catch (IOException e) {

154. }

155. }

156.

157. public String fixNomer(String no) {

158. //dari format no gini ”sms://+6285XXXX” jadi ”+6285XXX” gitu fungsinya disini itu

159.

160. String hs = null;

161. hs = no.substring(6, no.length());

162. return hs;

163. }

164.

165. public void commandAction(Command c, Displayable d) {

166. if (c == exitCom) {

167. destroyApp(true);

168. }

169. if (d == menuList) {

170. if (menuList.isSelected(0)) {

171. tulisSms();

172. } else if (menuList.isSelected(1)) {

173. Alert about = new Alert(“About”, “Aplikasi SMS buatan sendiri”,

174. null, AlertType.INFO);

175. display.setCurrent(about);

176. } else if (menuList.isSelected(2)) {

177. destroyApp(true);

178. }

179. }

180. if (d == textSmsKeluar) {

181. if (c == sendCom) {

182. form.deleteAll();

183. masukNo();

184. } else if (c == clrCom) {

185. textSmsKeluar.setString(“”);

186. } else if (c == backCom) {

187. display.setCurrent(menuList);

188. }

189. }

190. if (d == textSmsMasuk) {

191. if (c == exitCom) {

192. display.setCurrent(menuList);

193. } else if (c == replyCom) {

194. tulisSms();

195. }

196. }

197. if (d == form) {

198. if (c == backCom) {

199. form.deleteAll();

200. display.setCurrent(textSmsKeluar);

201. } else if (c == sendCom) {

202. isiSmsKeluar = textSmsKeluar.getString();

203. textSmsKeluar.setString(“”);

204. kirim();

205. }

206. }

207. }

208.

209. public void run() { //untuk menerima sms yang masuk

210. try {

211. msg = smsconn.receive();

212. String senderAddress = msg.getAddress();

213. Date waktuSms = msg.getTimestamp();

214. if (msg instanceof TextMessage) {

215. String isiSmsMasuk = ((TextMessage) msg).getPayloadText();

216. terima(isiSmsMasuk, senderAddress, waktuSms);//ini memasukan sms yg diterima ke textbox

217. }

218. tutupKoneksi();

219. bukaKoneksi();

220. } catch (IOException e) {

221. }

222. }

223.

224. public void notifyIncomingMessage(MessageConnection conn) {

225. if (thread == null) {

226. done = false;

227. thread = new Thread(this);

228. thread.start();

229. }

230. } }

Try if the problem can tell how SMS ... ..

0 comments:

Post a Comment