Translation Sun Help File - Javax.wireless.Messaging (Messagelistener)

zhaozj2021-02-16  50

Javax.wireless.Messaging Interface Messagelistener

The MessageListener interface provides a mechanism that allows the program to get notified when there is information.

When some information arrives, NotifyInComingMessage () will be called. The program must receive information with MessageConnection's Receive () method. MessageListener should not directly call Receive (), and a new thread should be used, or call a method that does not implement within this interface to indirectly call Receive (). For example, a simple MessageListener implementation will show how to use MessageListener.

The mechanism provided by this interface makes the program not need to hang the thread in the Receive () method.

If there is more than one information to arrive at a close time, the implementation of this interface will be notified synchronously. The program must synchronize some parts to process the above.

______________________________________________________________

Simple MessageListener implementation

The following code shows how to use MessageListener. In this example, a separate thread is used to process the reception of the information.

import java.io.IOException; import javax.microedition.midlet *;. import javax.microedition.io *;. import javax.wireless.messaging *;. public class Example extends MIDlet implements MessageListener {MessageConnection messconn; boolean done; Reader reader ; // Initialize public void startapp () {try {// Establish connection messageconn = (messageConnection) Connector.Open ("SMS: //: 6222"); // Register Listener Messconn.setMesetageListener (this); / / Establishment Information thread DONE = false; reader = new reader (); new thread (reader) .Start ();} catch (ooException e) {// Processing Error}} // Synchronization Public Void NotifyInsingMessage (MessageConnection Conn {IF CONN == Messconn) {reader.handleMessage ();}} // Close connection // Stop thread public void pauseapp () {done = true; try = true; try {mess (ooException e) { //}} // MIDlet processing error stop // @param unconditional forced shutdown flag public void destroyApp (boolean unconditional) {done = true; try {messconn.setMessageListener (null); messconn.close ();} catch (IOException e {// error handling}} // Use additional thread processing information reception // notifyIncomingMessage () quickly returns Class Reader Implements Runnable {Private INT PENDINGESS = 0; // Processing the actual work of receiving information PUBLIC VOID RUN ) {While (! "{Ix (pendingmessages ==

0) {Try {Wait ();} catch (exception e) {// handling error}}} // Used MessageListener benefits // This thread can be activated by the same mechanism to handle other events / / receive information not just try {message mess = messconn.receive ();} catch (IOException ioe) {// handle errors}}} public synchronized void handleMessage () {pendingMessages ; notify ();}}} ________________________________________________________________________

Method detail

NotifyNComingMessage Public Void NotifyIncomingMessage (MessageConnce Conn)

When the information arrives by the system, each message will be called.

Tip: The implementation of this method must return quickly and should not do any unnecessary operations. The program should not receive or process information in this method. It should only stimulate the thread of the program processing information.

Parameters: conn: MessageConnction of information arriving.

转载请注明原文地址:https://www.9cbs.com/read-23706.html

New Post(0)