HanG321 Blog
Be Shine, Be Smile, Be Wild
  • Home
  • Blog
    • 好文閱讀 readings
    • 生活記事 diary
    • 時事評論 commentary
    • 科技資訊 technology
    • 電腦編程 programming
    • 金融財經 finance
    • 音樂電影 music/movie
  • About

J2ME SMS

March 14, 2006|電腦編程

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

Java
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

Java
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

Java
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?
}
}

 

嘖香檳

March 12, 2006|生活記事

星期四, 有個死人頭逃避返公司….走上黎我到蛇王….. 仲係度訓!! 雖知我張床唔係咁隨便俾人上的 🙂

星期五, shaw photo day, assembly 遇上頒船…..愈想早走…愈係遲放….
嘖香檳, 自己唔係好濕, 不過見到matthew 完全冇濕過…忍不住”槍口對外” 的協議~~
遇見老朋友, 本想多談一會….. 咁岩又有電話 ……
近d 一、兩年好少聯絡……有點陌生了…..可能這就是………

星期六, 冇節目…… 做左一輪fyp….. 很少進展….轉戰418…更加無從入手

送禮物……都幾…..都唔知送乜好tim……..

中學

March 10, 2006|生活記事

久違了的感覺……很久沒有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 去俾藉口自己…… 電腦壞了, 學校的又壞了? 組員的又壞了? 是心態的問題吧…….

...102030...128129130131...160...

 

如果你喜歡我的文章,請幫忙按 1-10次 LikeButton 化讚為賞,非常感謝!越喜歡當然可以越按越多 😛

搜尋 Search

簡介 Bio

香港人,現居南十字星空下。

為人貪心,科技、生活、財經、散文 皆有興趣,周身刀冇張利。

思想矛盾,喜歡現在work-life balance 既生活又懷念a city never sleep。

 

每月送我一杯咖啡支持我: liker.land/hang321




分類 Categories

  • 好文閱讀
  • 時事評論
  • 未分類
  • 生活記事
  • 科技資訊
  • 金融財經
  • 電腦編程
  • 音樂電影

文章存檔 Archives




熱門文章 Popular Posts

  • Install XPEnology (DSM) 5.1 on ESXi 6 (HP MicroServer Gen 8)
    Install XPEnology (DSM) 5.1 on ESXi 6 (HP MicroServer Gen 8) June 8, 2015
  • 呢幾日個blogger 有問題….
    呢幾日個blogger 有問題…. October 28, 2004
  • assembly
    assembly February 11, 2006
  • 新工作
    新工作 January 6, 2009
  • 嫁人要嫁工程師
    嫁人要嫁工程師 April 27, 2006

標籤雲 Tag Cloud

CentOS Character chroot Cluster crash cryptography DD-WRT debug Domino DSM Dual Core DWA email ESXi GCP git google HylaFax IE Java Javascript JRE LikeCoin Linux log LotusScript mint MX MySQL nginx PKI PowerShell Qwiklabs srt telent VMware vpn vSphere WinXP wordpress XPEnology 專欄 網絡資訊 選股 風帆

日曆 Calendar

May 2025
M T W T F S S
  « Feb    
 1234
567891011
12131415161718
19202122232425
262728293031  

Follow Me

Follow Us on RSSFollow Us on TwitterFollow Us on YouTube

文章存檔 Archives

Copyright © 2004-2021 hang321.net. All Rights Reserved