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

📄 smssender.java

📁 J2ME核心类及MIDlet类 MIDP用户界面对象 图形处理及低级事件处理 多线程编程 I/O及网络编程 数据库RMS编程 浮点数编程 多媒体及GAME API编程 安全、加密及
💻 JAVA
字号:
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;

import java.io.IOException;

public class SMSSender 
    implements CommandListener, Runnable {

	Command sendCommand = new Command("发送", Command.OK, 1);
	Command backCommand = new Command("取消", Command.BACK, 2);
	Display display;
	String smsPort;
	String destinationAddress;
	TextBox messageBox;
	Displayable backScreen;
	Displayable sendingScreen;
	
	public SMSSender(String smsPort, Display display,
		Displayable backScreen, Displayable sendingScreen) {
		this.smsPort = smsPort;
		this.display = display;
		this.destinationAddress = null;
		this.backScreen = backScreen;
		this.sendingScreen = sendingScreen;

		messageBox = new TextBox("输入消息", null, 65535, TextField.ANY);
		messageBox.addCommand(backCommand);
		messageBox.addCommand(sendCommand);
		messageBox.setCommandListener(this);
	}

	public void promptAndSend(String destinationAddress)
	{
		this.destinationAddress = destinationAddress;
		display.setCurrent(messageBox);
	}
	
	public void commandAction(Command c, Displayable s) {
		try {
			if (c == backCommand) {
				display.setCurrent(backScreen);
			} else if (c == sendCommand) {
				display.setCurrent(sendingScreen);
				new Thread(this).start();	
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void run() {
		String address = destinationAddress + ":" + smsPort;
		   
		MessageConnection smsconn = null;
		try {
			/** 打开连接 */
			smsconn = (MessageConnection)Connector.open(address);

			TextMessage txtmessage = (TextMessage)smsconn.newMessage(
				MessageConnection.TEXT_MESSAGE);
			txtmessage.setAddress(address);
			txtmessage.setPayloadText(messageBox.getString());
			smsconn.send(txtmessage);
		} catch (Throwable t) {
			t.printStackTrace();
		}
		if (smsconn != null) {
			try {
				smsconn.close();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}				
		}
	}
}

⌨️ 快捷键说明

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