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

📄 mdtapis.java

📁 这个项目是在手机上发送SMS短信和接收SMS短信.使用标准的J2ME写的,因些有超平台的功能.经过Blackberry 8800 手机测试.
💻 JAVA
字号:
package com.webtechwireless.mobile.personallocator.communication;

import net.rim.device.api.system.*;
import javax.microedition.io.*;

import com.webtechwireless.mobile.personallocator.utils.TaskTimer;

import java.io.*;
import java.util.TimerTask;
import javax.wireless.messaging.*;

public class MDTApis implements MDTInterface {

	// private static long PL_ID = 0x33abf322367f9119L;
	// private static String SMSURL= "sms://:";
	private static String SMS_URL = "sms://:3737";
	private static String PL_APP = "com.webtechwireless.mobile.personallocator.PersonalLocator";

	private static final int DRIVER_LOGIN = 1;
	private static final int DRIVER_LOGOFF = 2;

	private final MessageCommunicator messageCommunicator;

	private String[] connections;
	private MessageConnection smsConn;
	private SMSTimer timer;

	public MDTApis(MessageCommunicator messageCommunicator) {
		this.messageCommunicator = messageCommunicator;

		registerSMS();

	}// MDTApis

	private void registerSMS() {
		connections = PushRegistry.listConnections(false);

		if (connections.length == 0) {
			try {
				// dconn = (UDPDatagramConnection)
				// Connector.open("datagram://");
				// String dport = "datagram://:" + dconn.getLocalPort();

				// Register the port so the MIDlet will wake up, if messages
				// are posted after the MIDlet exits.
				PushRegistry.registerConnection(SMS_URL, PL_APP, "*");

				connections = PushRegistry.listConnections(false);
				if (connections == null)
					return;
				final int size = connections.length;

				for (int i = connections.length - 1; i >= 0; --i) {
					if (connections[i].equals(SMS_URL)){
					smsConn = (MessageConnection) Connector
							.open(connections[i]);
					 }
				}

			} catch (ConnectionNotFoundException e) {
				System.out.println("ConnectionNotfoundException");
				return;
			} catch (IOException ioe) {

				System.out.println("IOException");
				return;
			} catch (ClassNotFoundException cnfe) {
				System.out.println("Class not found Exceptione");
				return;
			}

			catch (Exception e) {
				System.out.println("Exception");
				return;
			}

		}
		timer = new SMSTimer();
		timer.start();
		
	}

	private void getSMS() {
		String[] connectionsWithData = PushRegistry.listConnections(true);

		// Test all connections to locate the one matching "sms://5678".
		for (int i = connectionsWithData.length - 1; i >= 0; --i) {
			if (connectionsWithData[i].equals(SMS_URL)) {
				fetchData(SMS_URL);
			}
		}

	}

	private void fetchData(String connection) {
		try {
			// Open the connection.
			Connection c = Connector.open(connection);

			if (c instanceof MessageConnection) {
				// Open a MessageConnection
				MessageConnection mc = (MessageConnection) c;
				TextMessage txtMsg = (TextMessage) mc.receive();

				System.out.println("Received: " + txtMsg.ToString() + ", type="
						+ txtMsg.getMessageType());
				// getPayloadText());
			}

		} catch (IOException e) {
			System.out.println("IOException");
		}

	}

	private class SMSTimer extends TaskTimer {

		private boolean isTimerStart = false;

		public void start() {
			try {
				super.start(2, 2);
				isTimerStart = true;
			} catch (IllegalStateException ise) {
				System.out.println("TravelStartTimer.IllegalState Exception");

			}
		}

		public void stop() {
			super.stop();
			isTimerStart = false;
		}

		public TimerTask getTask() {
			final TimerTask saveTask = new TimerTask() {

				public void run() {
					getSMS();
				}
			};
			return saveTask;
		}// run()

		public boolean isTimerStart() {
			return isTimerStart;
		}
	}// eof TravelStartTimer

	public void close() {
		try {
			if (smsConn != null) {
				smsConn.close();
				smsConn = null;
			}
		} catch (Exception e) {
		}
	}

	public void sendDriverLogin(String driverId) {
		sendDriverLogMessage(driverId, DRIVER_LOGIN);
	}

	public void sendDriverLogOff(String driverId) {
		sendDriverLogMessage(driverId, DRIVER_LOGOFF);
	}

	private void sendDriverLogMessage(String driverId, int messageType) {
		String extension = "";
		switch (messageType) {
		case DRIVER_LOGIN:
			extension = "fwd=yes appid=* log=-1," + driverId
					+ ",Login msg=s:Login琺:,,,,";
			break;
		case DRIVER_LOGOFF:
			extension = "fwd=yes appid=* log=-1," + driverId
					+ ",Logout msg=s:Logout琺:,,,,";
			break;
		}

		messageCommunicator.sendLocationRecord(
				ReasonCode.REASON_CODE_DRIVER_LOG, extension);
	}

}// class MDTApis

⌨️ 快捷键说明

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