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

📄 smssend.java

📁 用Java/C#开发手机程序及移动应用光盘代码。J2ME核心类及MIDlet类;基于Java开发MIDlet程序;图形处理及低级事件处理;多线程编程;即时消息传送;I/O及网络编程;数据库编程;多媒体
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;

import java.io.IOException;

public class SMSSend extends MIDlet 
	implements CommandListener {

	Command exitCommand  = new Command("Exit", Command.EXIT, 2);
	Command okCommand = new Command("OK", Command.OK, 1);
	Display display;
	String smsPort;
	TextBox destinationAddressBox;
	Alert errorMessageAlert;
	Alert sendingMessageAlert;
	SMSSender sender;
	Displayable resumeScreen = null;
	
	public SMSSend() {
		smsPort = getAppProperty("SMS-Port");

		display = Display.getDisplay(this);

		destinationAddressBox = new TextBox("目标地址", 
			null, 256, TextField.PHONENUMBER);
		destinationAddressBox.addCommand(exitCommand);
		destinationAddressBox.addCommand(okCommand);
		destinationAddressBox.setCommandListener(this);
		
		errorMessageAlert = new Alert("SMS", null, null, AlertType.ERROR);
		errorMessageAlert.setTimeout(5000);
	
		sendingMessageAlert = new Alert("SMS", null, null, AlertType.INFO);
		sendingMessageAlert.setTimeout(5000);
		sendingMessageAlert.setCommandListener(this);
		
		sender = new SMSSender(smsPort, display, destinationAddressBox, 
			sendingMessageAlert);
			
		resumeScreen = destinationAddressBox;
	}

	public void startApp() {
		display.setCurrent(resumeScreen);
	}

	public void pauseApp() {
		resumeScreen = display.getCurrent();
	}

	public void destroyApp(boolean unconditional) {
	}

	public void commandAction(Command c, Displayable s) {
		try {
			if (c == exitCommand || c == Alert.DISMISS_COMMAND) {
				destroyApp(false);
				notifyDestroyed();
			} else if (c == okCommand) {
				promptAndSend();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	private void promptAndSend() {
		String address = destinationAddressBox.getString();
		if (!SMSSend.isValidPhoneNumber(address)) {
			errorMessageAlert.setString("电话号码");
			display.setCurrent(errorMessageAlert, destinationAddressBox);
			return;
		}
		String statusMessage = "发消息给 " + address + "...";
		sendingMessageAlert.setString(statusMessage);
		sender.promptAndSend("sms://" + address);
	}
	
	private static boolean isValidPhoneNumber(String number) {
		char[] chars = number.toCharArray();
		if (chars.length == 0) {
			return false;
		}
		int startPos = 0;
		if (chars[0]=='+') {
			startPos = 1;
		}
		for (int i = startPos; i < chars.length; ++i) {
			if (!Character.isDigit(chars[i])) {
				return false;
			}
		}
		return true;
	}
}

⌨️ 快捷键说明

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