⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j2me 消息传递应用程序.txt

📁 J2ME 消息传递应用程序 在该结构中
💻 TXT
字号:
 J2ME 消息传递应用程序 









在该结构中,使用监听器模式实现了一个基础的框架,主要包括以下几个类和接口:

1、HTTPThread——实现网络操作的功能类,实现网络的连接

2、HTTPListener——网络监听器实现

3、LoginForm——登录功能实现结构





  在这一节里,我将演示一个 WMAServer 示例,它等候进入的 SMS 消息,然后将它们显示在电话屏幕上。javax.microedition.lcdui 包提供了一组功能,用于实现应用程序的用户界面。 

  WMAServer MIDlet 通过将标识本地主机上端点(取决于协议的标识符,例如,端口号)的字符串传递给 Connector.open() 方法来创建服务器方式连接。 

  为了能收到进入消息的通知,MIDlet 在 MessageConnection 实例 serverConn 处注册一个 MessageListener 对象。 

serverConn.setMessageListener(MessageListener ml);
 


  它还在 MessageListener 接口中实现 notifyIncomingMessage()。当进入消息到达 MessageConnection 时,就调用 notifyIncomingMessage() 方法。应用程序必须使用 MessageConnection 的 receive() 方法来检索该消息。 

  WMAServer 应用程序从进入消息读取文本或二进制的有效负载数据,然后将其存储在字符串对象中供以后显示。 

public void notifyIncomingMessage(MessageConnection conn) {
        Message msg = null;
        //  Try reading (maybe block for) a message
        try {
            msg = conn.receive();
        }
        catch (Exception e) {
            // Handle reading errors
            System.out.println("processMessage.receive " + e);
        }
        // Process the received message
        if (msg instanceof TextMessage) {
            TextMessage tmsg = (TextMessage)msg;
            msgReceived = tmsg.getPayloadText();
        }
        else
        {
            // process received message
            if (msg instanceof BinaryMessage) {
                BinaryMessage bmsg = (BinaryMessage)msg;
                byte[] data = bmsg.getPayloadData();
                //  Handle the binary message...
                msgReceived = data.toString();
        }
}
 


  当必须释放连接资源和相关联的侦听器对象时,应用程序提供 destroyApp() 方法。 

public void destroyApp(boolean unconditional) {
  try {
    if (serverConn != null) {
        serverConn.setMessageListener(null);
        serverConn.close();
    }
}
catch (IOException e) {
    //  Handle the exception...
    e.printStacktrace();
}
 


  以下是完整的应用程序代码: 

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.*;
import java.lang.*;
import java.util.*;
//A first MIDlet with simple text and a few commands.
public class WMAExample extends MIDlet
               implements CommandListener, MessageListener  {
  //The exit commands
  private Command exitCommand;
  //Command to get Message
  private Command getMsgCommand;
  //The display for this MIDlet
  private Display display;
  Form displayForm;
  String msgReceived;
  MessageConnection serverConn;
  public WMAExample() {
    display = Display.getDisplay(this);
    exitCommand =
    new Command("Exit", Command.SCREEN, 1);
    getMsgCommand =
    new Command("Get", Command.SCREEN, 1);
 }
 // Start the MIDlet by creating two command buttons
  public void startApp() {
     displayForm = new Form("Get Message");
     displayForm.addCommand(exitCommand);
     displayForm.addCommand(getMsgCommand);
     displayForm.setCommandListener(this);
     displayForm.setItemStateListener(this);
     display.setCurrent(displayForm);
     try
     {
     serverConn = (MessageConnection)Connector.open
     ("sms://:5000");
     // Register the listener for inbound messages.
     serverConn.setMessageListener(this);
     }
     catch (IOException ioExc)
     {
     System.out.println("Server connection could
     not be obtained");
     ioExc.printStackTrace();
     }
  }
  public void notifyIncomingMessage(MessageConnection
  conn) {
        Message msg = null;
        //  Try reading (maybe block for) a message
        try {
            msg = conn.receive();
        }
        catch (Exception e) {
            // Handle reading errors
            System.out.println("processMessage.receive "
            + e);
        }
        // Process the received message
        if (msg instanceof TextMessage) {
            TextMessage tmsg = (TextMessage)msg;
            msgReceived = tmsg.getPayloadText();
        }
        else
        {
            // process received message
            if (msg instanceof BinaryMessage) {
                BinaryMessage bmsg = (BinaryMessage)msg;
                byte[] data = bmsg.getPayloadData();
                //  Handle the binary message...
                msgReceived = data.toString();
        }
}
  // Pause is a no-op because there are no   background
  // activities
  public void pauseApp() { }
  // Destroy must cleanup everything not handled
  // by the garbage collector.
  public void destroyApp(boolean unconditional) {
  try {
    if (serverConn != null) {
        serverConn.setMessageListener(null);
        serverConn.close();
    }
}
catch (IOException e) {
    //  Handle the exception...
    e.printStacktrace();
}
}
 // Respond to commands.
  public void commandAction(
  Command c, Displayable s) {
    if (c == exitCommand) {
        destroyApp(false);
        notifyDestroyed();
    }
    if (c == getMsgCommand) {
        try
        {
            displayForm.append(msgReceived);
            display.setCurrent(displayForm);
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }
  }
 }
 


  接下来是一个 WMA 客户机示例,该客户机向 GSM 移动电话发送一条 SMS 消息。WMA 客户机只能发送消息。它通过向 Connector.open() 方法传递一个标识目的地地址的字符串(一个有效的手机号码)来创建 MessageConnection。试试: 

{
     clientConn = (MessageConnection)Connector.open("sms://
     +18643630999:5000");
     }
     catch (IOException ioExc)
     {
     System.out.println("Client connection could not be obtained");
     ioExc.printStackTrace();
     }
 


  它随后通过设置有效负载和目的地地址来创建有效的 TextMessage 对象,并通过 MessageConnection 发送消息。 

TextMessage tmsg =
            (TextMessage)clientConn.newMessage
              (MessageConnection.TEXT_MESSAGE);
            tmsg.setAddress("sms://18643630999:5000");
            tmsg.setPayloadText(msgToSend);
            clientConn.send(tmsg);
 


  以下是 WMA 客户机的完整应用程序代码: 

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.*;
import java.lang.*;
import java.util.*;
//A first MIDlet with simple text and a few commands.
public class WMAClient extends MIDlet
               implements CommandListener, MessageListener  {
  //The exit commands
  private Command exitCommand;
  //Command to get Message
  private Command sendMsgCommand;
  //The display for this MIDlet
  private Display display;
  Form displayForm;
  String msgToSend="Can you hear me?";
  MessageConnection clientConn;
  public WMAClient() {
    display = Display.getDisplay(this);
    exitCommand =
    new Command("Exit", Command.SCREEN, 1);
    sendMsgCommand =
    new Command("Send", Command.SCREEN, 1);
 }
public void notifyIncomingMessage(MessageConnection conn)
 {
 }
 // Start the MIDlet by creating the TextBox and
  // associating the exit command and listener.
  public void startApp() {
     displayForm = new Form("Send Message");
     displayForm.addCommand(exitCommand);
     displayForm.addCommand(sendMsgCommand);
     displayForm.setCommandListener(this);
     displayForm.setItemStateListener(this);
     display.setCurrent(displayForm);
     try
     {
     clientConn = (MessageConnection)Connector.open("sms://
     +18643630999:5000");
     }
     catch (IOException ioExc)
     {
     System.out.println("Client connection could
     not be obtained");
     ioExc.printStackTrace();
     }
  }
  // Pause is a no-op because there is no   background
  // activities or record stores to be closed.
  public void pauseApp() { }
  // Destroy must cleanup everything not handled
  // by the garbage collector.
  public void destroyApp(boolean unconditional) { }
 // Respond to commands.
  public void commandAction(
  Command c, Displayable s) {
    if (c == exitCommand) {
        destroyApp(false);
        notifyDestroyed();
    }
    if (c == sendMsgCommand) {
        try
        {
            TextMessage tmsg =
            (TextMessage)clientConn.newMessage
              (MessageConnection.TEXT_MESSAGE);
            tmsg.setAddress("sms://18643630999:5000");
            tmsg.setPayloadText(msgToSend);
            clientConn.send(tmsg);
        }
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }
  }
 }
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -