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

📄 cservice.java

📁 开源的手机短信开发包!包括例子程序和比较详细的文档
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
					deviceInfo.getStatistics().incTotalIn();
				}
				line = reader.readLine().trim();
			}
			reader.close();
		}
		else throw new NotConnectedException();
	}

	/**
		Send an SMS message from the GSM device. Once connected, you can create a
		COutgoingMessage object with the message you want to send, and pass it
		to this function.
		<br><br>
		<strong>Notes:</strong>
		<ul>
			<li>If you have set a phonebook, you can create the COutgoingMessage
				object with a nickname, instead of the actual phone number.</li>
		</ul>

		@param	message	a COutgoingMessage containing the message you wish to send.
		@return  true if sending succeded.
		@see	COutgoingMessage
		@see	CPhoneBook
		@see	CService#sendMessage(LinkedList)
		@see	CService#setPhoneBook(String)
	*/
	public boolean sendMessage(COutgoingMessage message) throws Exception
	{
		LinkedList messageList;
		COutgoingMessage msg;
		boolean result;

		messageList = new LinkedList();
		messageList.add(message);
		sendMessage(messageList);
		return (message.getDispatchDate() != null);
	}


	/**
		Send an series of SMS messages from the GSM device. This method is used
		when you want to send more than one message as a batch. If your GSM device
		support the feature of keeping the GSM link open during message dispatch,
		this method should work faster than calling the sendMessage(COutgoingMessage)
		method many times.
		<br>
		Just create a LinkedList object, add as many COutgoingMessage objects you wish
		and call the method.
		<br><br>
		<strong>Notes:</strong>
		<ul>
			<li>If you have set a phonebook, you can create the COutgoingMessage
				object with a nickname, instead of the actual phone number.</li>
		</ul>

		@param	messageList	a LinkedList filled with COutgoingMessage objects.
		@throws NotConnectedException
		@see	COutgoingMessage
		@see	CPhoneBook
		@see	CService#sendMessage(COutgoingMessage)
		@see	CService#setPhoneBook(String)
	*/
	public void sendMessage(LinkedList messageList) throws Exception
	{
		LinkedList outList;
		COutgoingMessage message;
		int i, j;
		String pdu;

		if (getConnected())
		{
			if (phoneBook.isLoaded()) outList = phoneBook.expandPhoneBookEntries(messageList);
			else outList = messageList;
			atHandler.keepGsmLinkOpen();
			for (i = 0; i < outList.size(); i ++)
			{
				message = (COutgoingMessage) outList.get(i);
				pdu = message.getPDU(smscNumber);
				j = pdu.length();
				j /= 2;
				if (smscNumber == null) ;
				else if (smscNumber.length() == 0) j --;
				else
				{
					j -= ((smscNumber.length() - 1) / 2);
					j -= 2;
				}
				if (atHandler.sendMessage(j, pdu))
				{
					message.setDispatchDate(new Date());
					deviceInfo.getStatistics().incTotalOut();
				}
				else message.setDispatchDate(null);
			}
		}
		else throw new NotConnectedException();
	}

	/**
		Deletes an SMS message from the GSM device memory.
		<br><br>
		<strong>Notes:</strong>
		<ul>
			<li>A deleted message cannot be recovered.</li>
			<li>It is highly recommended to use the other form of the deleteMessage()
				method.</li>
		</ul>

		@param	memIndex	the memory index of the GSM device's memory from where
				the message (if there is any message there) should be deleted.
		@return  True if delete operation succeded.
		@throws NotConnectedException
		@see	CService#deleteMessage(CIncomingMessage)
	*/
	public boolean deleteMessage(int memIndex) throws Exception
	{
		String response;

		if (getConnected())
		{
			if (memIndex > 0)	return atHandler.deleteMessage(memIndex);
			else return false;
		}
		else throw new NotConnectedException();
	}

	/**
		Deletes an SMS message from the GSM device memory.
		<br><br>
		<strong>Notes:</strong>
		<ul>
			<li>A deleted message cannot be recovered.</li>
		</ul>

		@param	message	a valid CIncomingMessage object, i.e. an object which is
				previously read with readMessages() from the GSM device.
		@return  True if delete operation succeded.
		@throws NotConnectedException
		@see	CIncomingMessage
		@see	CService#deleteMessage(int)
	*/
	public boolean deleteMessage(CIncomingMessage message) throws Exception
	{
		return deleteMessage(message.getMemIndex());
	}

	/**
		Sets the preferred message storage of the GSM device to SIM memory.

		@return  true if the change of preferred message storage succeded.
	*/
	public boolean setStorageSIM() throws Exception
	{
		return atHandler.setStorageSIM();
	}

	/**
		Sets the preferred message storage of the GSM device to build-in memory.

		@return  true if the change of preferred message storage succeded.
	*/
	public boolean setStorageMEM() throws Exception
	{
		return atHandler.setStorageMEM();
	}

	/**
		Refreshes the GSM device specific information. This method is called once during
		connection. Its up to the developer to call it periodically in order to get the latest
		information.

		Note: Information about Manufacturer, Model, SerialNo, IMSI, S/W Version are
		refreshed only once, since they don't change during a session.

		@see	CDeviceInfo
		@see	CService#connect()
		@see	CService#getDeviceInfo()
	*/
	public void refreshDeviceInfo() throws Exception
	{
		if (getConnected())
		{
			if (deviceInfo.manufacturer.length() == 0) deviceInfo.manufacturer = getManufacturer();
			if (deviceInfo.model.length() == 0) deviceInfo.model = getModel();
			if (deviceInfo.serialNo.length() == 0) deviceInfo.serialNo = getSerialNo();
			if (deviceInfo.imsi.length() == 0) deviceInfo.imsi = getImsi();
			if (deviceInfo.swVersion.length() == 0) deviceInfo.swVersion = getSwVersion();
			deviceInfo.batteryLevel = getBatteryLevel();
			deviceInfo.signalLevel = getSignalLevel();
		}
		else throw new NotConnectedException();
	}

	private String getManufacturer() throws Exception
	{
		String response;
		StringTokenizer tokens;

		String whatToDiscard;

		response = atHandler.getManufacturer();
		if (response.indexOf("OK") > -1)
		{
			tokens = new StringTokenizer(response, "\r");
			return tokens.nextToken();
		}
		else return VALUE_NOT_REPORTED;
	}

	private String getModel() throws Exception
	{
		String response;
		StringTokenizer tokens;

		String whatToDiscard;

		response = atHandler.getModel();
		if (response.indexOf("OK") > -1)
		{
			tokens = new StringTokenizer(response, "\r");
			return tokens.nextToken();
		}
		else return VALUE_NOT_REPORTED;
	}

	private String getSerialNo() throws Exception
	{
		String response;
		StringTokenizer tokens;

		String whatToDiscard;

		response = atHandler.getSerialNo();
		if (response.indexOf("OK") > -1)
		{
			tokens = new StringTokenizer(response, "\r");
			return tokens.nextToken();
		}
		else return VALUE_NOT_REPORTED;
	}

	private String getImsi() throws Exception
	{
		String response;
		StringTokenizer tokens;

		String whatToDiscard;

		response = atHandler.getImsi();
		if (response.indexOf("OK") > -1)
		{
			tokens = new StringTokenizer(response, "\r");
			return tokens.nextToken();
		}
		else return VALUE_NOT_REPORTED;
	}

	private String getSwVersion() throws Exception
	{
		String response;
		StringTokenizer tokens;

		String whatToDiscard;

		response = atHandler.getSwVersion();
		if (response.indexOf("OK") > -1)
		{
			tokens = new StringTokenizer(response, "\r");
			return tokens.nextToken();
		}
		else return VALUE_NOT_REPORTED;
	}

	private int getBatteryLevel() throws Exception
	{
		String response;
		StringTokenizer tokens;

		String whatToDiscard;

		response = atHandler.getBatteryLevel();
		if (response.indexOf("OK") > -1)
		{
			tokens = new StringTokenizer(response, "\r:,");
			tokens.nextToken(); tokens.nextToken();
			return Integer.parseInt(tokens.nextToken());
		}
		else return 0;
	}

	private int getSignalLevel() throws Exception
	{
		String response;
		StringTokenizer tokens;

		String whatToDiscard;

		response = atHandler.getSignalLevel();
		if (response.indexOf("OK") > -1)
		{
			tokens = new StringTokenizer(response, "\r:,");
			tokens.nextToken();
			return (Integer.parseInt(tokens.nextToken().trim()) * 100 / 31);
		}
		else return 0;
	}

	/**
		Checks if the message is SMS-DELIVER.

		@param pdu the message pdu
		@return true if the message is SMS-DELIVER
	*/
	private boolean isIncomingMessage(String pdu)
	{
		int index, i;

		i = Integer.parseInt(pdu.substring(0, 2), 16);
		index = (i + 1) * 2;

		i = Integer.parseInt(pdu.substring(index, index + 2), 16);
		if ((i & 0x03) == 0) return true;
		else return false;
	}

	/**
		Checks if the message is SMS-STATUS-REPORT.

		@param pdu the message pdu
		@return true if the message is SMS-STATUS-REPORT
	*/
	private boolean isStatusReportMessage(String pdu)
	{
		String str;
		int index, i;

		str = pdu.substring(0, 2);
		i = Integer.parseInt(str, 16);
		index = (i + 1) * 2;

		str = pdu.substring(index, index + 2);
		i = Integer.parseInt(str, 16);
		if ((i & 0x02) == 2) return true;
		else return false;
	}

	/**
		Virtual method, called upon receipt of a message (Asynchronous mode only!)
		<br><br>
		<strong>Notes:</strong>
		<ul>
			<li>If you plan to use jSMSEngine API in asynchronous mode, you should
				override this method, making it do your job upon message receipt.</li>
		</ul>

		@param	message the received message.
		@return  return true if you wish the message to be deleted from the GSM device's memory.
					Otherwise false.
		@see	CService#setReceiveMode(int)
	*/
	public @Deprecated boolean received(CIncomingMessage message)
	{
		return false;
	}

	private class CReceiveThread extends Thread
	{
		private boolean stopFlag;
		private boolean stopped;

		public CReceiveThread()
		{
			stopFlag = false;
			stopped = false;
		}

		public void killMe() { stopFlag = true; }
		public boolean killed() { return stopped; }

		public void run()
		{
			LinkedList messageList;

			messageList = new LinkedList();
			while (!stopFlag)
			{
				try { sleep(5000); } catch (Exception e) {}
				if (stopFlag) break;
				if ((getConnected()) && (getReceiveMode() == RECEIVE_MODE_ASYNC))
				{
					messageList.clear();
					try
					{
						readMessages(messageList, CIncomingMessage.CLASS_ALL);
						for (int i = 0; i < messageList.size(); i ++)
						{
							CIncomingMessage message = (CIncomingMessage) messageList.get(i);
							if (getMessageHandler() == null)
							{
								if (received(message)) deleteMessage(message);
							}
							else
							{
								if (getMessageHandler() != null && getMessageHandler().received(CService.this, message)) deleteMessage(message);
							}

						}
					}
					catch (Exception e)
					{
						e.printStackTrace();
					}
				}
			}
			stopped = true;
		}
	}

	public static void main(String[] args)
	{
		System.out.println("jSMSEngine API.");
		System.out.println("	An open source Java API to process SMS messages from your ");
		System.out.println("	 mobile phone or gsm modem.");
		System.out.println("	This software is distributed under the LGPL license.");
		System.out.println("");
		System.out.println("Copyright (C) 2002-2006, Thanasis Delenikas, Athens / GREECE.");
		System.out.println("Visit http://www.jsmsengine.org for latest information.");
		System.out.println("\n");
		System.out.println(_name + " v" + _version + " { " + _reldate + " }");
	}
}

⌨️ 快捷键说明

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