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

J2ME SMS

March 14, 2006|電腦編程
Home » Blog » 電腦編程 » J2ME SMS

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 14, 2006 user

Recent Posts

  • Serverless Cloud Run Development: Challenge Lab
    Serverless Cloud Run Development: Challenge LabFebruary 7, 2021
  • ionic 4: capacitor & pwa
    ionic 4: capacitor & pwaJanuary 2, 2019
  • Open file limit of tomcat and mysql in Linux server
    Open file limit of tomcat and mysql in Linux serverDecember 1, 2018

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
  • assembly
    assemblyFebruary 11, 2006
  • 呢幾日個blogger 有問題….
    呢幾日個blogger 有問題….October 28, 2004
← 嘖香檳
J2ME radio button →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.




最新文章 Recent Posts

  • Serverless Cloud Run Development: Challenge Lab
  • ionic 4: capacitor & pwa
  • Open file limit of tomcat and mysql in Linux server
  • avoid subquery in mysql update statement
  • hacktoberfest

最新留言 Recent Comments

  • admin on LikeCoin
  • Edmond on LikeCoin
  • John Smith on Install macOS High Sierra on VMware
  • admin on Install macOS High Sierra on VMware
  • nana on Install macOS High Sierra on VMware




熱門文章 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

March 2006
M T W T F S S
  « Feb   Apr »  
 12345
6789101112
13141516171819
20212223242526
2728293031  

Follow Me

Follow Us on RSSFollow Us on TwitterFollow Us on YouTube

文章存檔 Archives

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