http://www-128.ibm.com/developerworks/wireless/library/wi-p2pmsg/ Send SMS messages Sending messages with the WMA is very simple. You can send a message to an arbitrary phone number and/or an SMS port through a MessageConnection constructed for that destination, as shown in Listing 1. Listing 1. Sending an SMS message
1 2 3 4 5 6 7 8 9 |
String addr = "sms://+123456789"; // Or: String addr = "sms://+123456789:1234"; MessageConnection conn = (MessageConnection) Connector.open(addr); TextMessage msg = (TextMessage) conn.newMessage( MessageConnection.TEXT_MESSAGE); msg.setPayloadText( "Hello World" ); conn.send(msg); |
We can also send out messages through a server connection, as shown in Listing 2. Listing 2. Sending an SMS message via a server connection
1 2 3 4 5 6 7 8 |
MessageConnection sconn = (MessageConnection) Connector.open("sms://:3333"); TextMessage msg = (TextMessage) sconn.newMessage( MessageConnection.TEXT_MESSAGE); msg.setAddress("sms://+123456789:1234"); msg.setPayloadText( "Hello World" ); sconn.send(msg); |
If you choose to use a server connection to send your message, you gain a number of benefits: The connection can be reused again and again. The message contains the sender’s port number and phone number. Hence, it gives the recipient peer the opportunity to respond. Receive SMS messages To receive SMS messages in a Java application, we need to have a server MessageConnection listening at the message’s target port. The MessageConnection.receive() method blocks until a message is received or the connection is closed. We can loop the receive() method to automatically handle incoming messages when they arrive. Listing 3 illustrates a server loop that listens, receives, and replies to incoming SMS messages. Listing 3. Receiving an SMS message and replying to it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
MessageConnection sconn = (MessageConnection) Connector.open("sms://:3333"); while (true) { Message msg = sconn.receive(); if (msg instanceof TextMessage) { TextMessage tmsg = (TextMessage) msg; String msgText = tmsg.getPayloadText(); // Construct the return message TextMessage rmsg = (TextMessage) sconn.newMessage( MessageConnection.TEXT_MESSAGE); rmsg.setAddress ( tmsg.getAddress() ); rmsg.setPayloadText( "Thanks!" ); sconn.send(rmsg); } else { // process the non-text message // maybe a BinaryMessage? } } |
星期四, 有個死人頭逃避返公司….走上黎我到蛇王….. 仲係度訓!! 雖知我張床唔係咁隨便俾人上的 🙂 星期五, shaw photo day, assembly 遇上頒船…..愈想早走…愈係遲放…. 嘖香檳, 自己唔係好濕, 不過見到matthew 完全冇濕過…忍不住”槍口對外” 的協議~~ 遇見老朋友, 本想多談一會….. 咁岩又有電話 …… 近d 一、兩年好少聯絡……有點陌生了…..可能這就是……… 星期六, 冇節目…… 做左一輪fyp….. 很少進展….轉戰418…更加無從入手 送禮物……都幾…..都唔知送乜好tim……..
久違了的感覺……很久沒有asg 100 pts 仲有 v.good 的紅筆字….有點回到中學的年代…. 對左功課先submit ….仲有成堂講answer …. 明明大家都唔在意的分數都扮得好在意般, 為左一個 98 有v.good, 一個都係98 但冇v.good 而討論 🙂 今日只係做左 2800 個份asg….. fyp, 418, ielts ….全部都冇掂過….. 享樂得太多了……. 玩左幾round PS2 winning, 都係pc version 好玩d ~~ 呢兩個week 真的很多次了….. 唔鍾意成日check 住check 住咁…..我已經唔係小朋友好耐……… 我明白有什麼的因, 有什麼的果, 但這改變..我真的適應不來……. 係我太仁慈吧……太過容易俾佢地delay…… 雖然今個星期六唔使出去的感覺好好:) . 不過唔代表可以用一d 好冇pt 的reason 去俾藉口自己…… 電腦壞了, 學校的又壞了? 組員的又壞了? 是心態的問題吧…….