Enterprise J2ME Developing Mobile Java Applications [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Enterprise J2ME Developing Mobile Java Applications [Electronic resources] - نسخه متنی

Michael Juntao Yuan

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



9.2 WMA in Action


The WMATester sample application, available from this book's Web site (see "Resources"), demonstrates the basic use of the WMA and the Sun Microsystems Reference Implementation (RI). You can use the provided ANT script to build and run the project. Commands ant run1 and ant run2 bring up two emulator windows that can send emulated SMS messages to each other (see Section 9.3.1). Peer 1 listens at SMS port 3333 and peer 2 listens at port 3334. So, if you want to send a message from peer 1 to peer 2, you can use an address like 18005555555:3334. Upon receipt of the message, peer 2 will display it. If the message comes from a server connection (using the Send2 button), peer 2 will send an acknowledgment back to peer 1. The process is illustrated in Figure 9.2.


Figure 9.2. WMATester work flow.


There are two MIDlets in the WMATester suite. They have identical functionalities from the user point of view. The difference is that WMAsync uses a synchronous messaging model and WMAasync uses an asynchronous model. In the next two sections, I walk you through basic features of WMA and its RI using code snippets from the WMATester.


9.2.1 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 9.1.

Listing 9.1. Sending an SMS message



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);

You can also send out messages through a server connection, as shown in Listing 9.2.

Listing 9.2. Sending an SMS message via a server connection



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.



9.2.2 Synchronously 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 9.3 illustrates a server loop that listens, receives, and replies to incoming SMS messages.

Listing 9.3. Receive and reply to all incoming SMS messages



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

Note

In the real world, the server loop in Listing 9.3 must run in a separate background thread to avoid blocking the user interface. To see how that works, please refer to our sample application, WMATester.


9.2.3 Asynchronously Receive SMS Messages


The polling loop using the receive() method is often tedious to program with. WMA provides an event-based model for incoming message processing. The following code snippet (Listing 9.4) is adopted from the WMATester example to illustrate the asynchronous messaging technique.

Listing 9.4. Asynchronously receive messages



// Implements the MessageListener IF
public class WMAasync extends MIDlet
implements CommandListener, MessageListener {
// Init a server connection and assign
// a listener for it.
public void startApp() {
try {
displayBlankForm ();
sconn = (MessageConnection) Connector.open("sms://:"+serverPort);
sconn.setMessageListener(this);
} catch (Exception e) {/* process error*/}
}
// ... ...
// Implement the message handler
// method required in the MessageListener
// interface.
public void notifyIncomingMessage(
MessageConnection c) {
// 'c' is sconn. No need to pass into
// the handling thread
new Thread(new SMSHandler()).start();
return;
}
class SMSHandler implements Runnable {
public void run () {
try {
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( "Message " +
msgText + " is received" );
sconn.send(rmsg);
// Display mesgText
} else {/* not a text mesg */}
} catch (Exception e) {/* handle error */}
}
}
}

Please note how to put message handling in a separate thread so that we can quickly return from the notifyIncomingMessage() method, as required by the specification. The handler thread is similar to Listing 9.4 except that we do not need the loop anymore.


9.2.4 Receive SMS Message via MIDP PUSH


Through the MIDP v2.0 PUSH architecture, we can push the concept of asynchronous messaging one step further. The MIDP 2.0 AMS can listen for incoming connections. When a registered connection comes in, the AMS invokes a corresponding MIDlet to handle it. This way, the user does not even need to manually start up the server MIDlet. When the AMS picks up an SMS incoming message, it first buffers the message and then triggers the handling MIDlet. The MIDlet can read and delete the message. For more information on the PUSH architecture, please refer to the MIDP 2.0 specification and WMA documentation (see "Resources").


/ 204