import java.io.IOException;
import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
/**
* <P>
* A standalone thread, which is used to send SMS to Ehoo SMS gateway. According
* to JSR 120, the address should be in the form of "sms://" + address_id.
* </P>
* <P>
* SMSSender provides the ability of sending text message to a destination
* address. User should get an instance of SMSSender via factory-method:
* SMSSender.getSMSSender().
* </P>
* <P>
* Once get the singleton instance, the user should set the destination address
* and payload text for this connection. After that, the main thread could be
* start() :)
* </P>
*
* @author Tommy Wang (V1), Jedi Chen (V2)
*/
public class SMSSender {
/** the string stores the text message that will be sent. */
private String m_szMessageTxt;
/**
* boolean variable indicates whether the SMS has been sent successfully.
* initialized as false when get the singleton instance.
*/
private static boolean m_fDoSuccessfully;
/**
* boolean variable indicates whether error occured while sending SMS.
* initialized as false when get the singleton instance.
*/
/** the destination address for current SMS sender. */
private String m_szAddress;// 发送的地址
/**
* the byte flag for error type, currently NOT used. If it's necessary to
* get the information about error type, we could use it. When exception is
* caught during sending process, we could determine the error type, and set
* corresponding bit flag. [JC]
*/
// private byte m_bErrorType;
/**
* the singleton instance of SMSSender, since one instance is enough for one
* MIDlet, we apply the Singleton pattern for this class.
*/
private static SMSSender m_spSingleton;
/**
* the Factory method to get the singleton instance.
*/
public static SMSSender getSMSSender() {
if (m_spSingleton == null) {
m_spSingleton = new SMSSender();
} else {
m_spSingleton.resetSenderStatus();
}
return m_spSingleton;
}
/**
* The private constructor for SMSSender, only could be called by
* getSMSSender.
*
* call resetSenderStatus() to reset the members.
*/
private SMSSender() {
resetSenderStatus();
}
public synchronized void setMessageText(String address, String s) {
// assert(address != null && !address.equals(""));
m_szAddress = "sms://" + address;
if (s == null || s.equals(""))
s = "[WARN] Error formatted message!";
m_szMessageTxt = s;
m_fDoSuccessfully = false;
System.out.println("[SMS] " + s);
}
/**
* Send the message in a standalone thread.
*
* @see java.lang.Runnable#run()
*/
public boolean send() {
MessageConnection smsconn = null;
try {
// System.out.println("54321");
smsconn = (MessageConnection) Connector.open(m_szAddress);
// System.out.println("12345");
TextMessage txtmsg = (TextMessage) smsconn
.newMessage(MessageConnection.TEXT_MESSAGE);
txtmsg.setAddress(m_szAddress);
txtmsg.setPayloadText(m_szMessageTxt);
smsconn.send(txtmsg);
m_fDoSuccessfully = true;
System.out.println("[SMS] SMS sent successfully :)");
} catch (IOException ex) {
m_fDoSuccessfully = false;
} catch (IllegalMonitorStateException ep) {
m_fDoSuccessfully = false;
} catch (SecurityException es) {
m_fDoSuccessfully = false;
} catch (Exception e) {
m_fDoSuccessfully = false;
} finally {
if (smsconn != null) {
try {
smsconn.close();
} catch (IOException ioex) {
System.out
.println("[SMS] Close SMS connection error caught!");
ioex.printStackTrace();
}
}
}
return m_fDoSuccessfully;
}
/**
* Once the caller get the sender status, it must call this method to reset
* both status.
*/
private void resetSenderStatus() {
m_fDoSuccessfully = false;
m_szAddress = null;
m_szMessageTxt = null;
}
/**
* @return Returns the doSuccessfully.
*/
public static synchronized boolean isDoSuccessfully() {
return m_fDoSuccessfully;
}
}
分享到:
相关推荐
自己编写的通过串口发送短信的源代码,使用MSCOMM控件,速度快,稳定!
COVID19-SMSSender 只要该选定国家/地区中有更多“总案件”,您就可以实时接收短信。 屏幕截图 要求 Twilio帐户 如何安装 在命令提示符中运行以下命令: pip install -r requirements.txt 根据您的需要编辑config....
一个发送SMS消息的类 这是一个用php编写的发送SMS消息的类,他可以连接SMS服务器,完成用户验证,并发送SMS信息到用户手机中
作为一个程序员,了解一些相关的这种技术也是必不可少的,此源码可以用java代码实现发送短信的功能,当然,首先得有账号和秘钥,可以到说明上去注册,java实现短信功能相信很快就会是你所拥有的知识。
$SMSSender->sendSMS("375297357355", "Hello, Brother", false); 将短信添加到 Doctrine 队列的代码示例: $SMSSender->sendSMS("375297357355", "Hello, Brother"); 用于添加到 Cron 的控制台命令 ...
短信 24x7 客户端一个简单的实用程序/模块,允许通过 sms24x7.ru SMS 服务发送...代码中的使用 var SmsSender , smsSender , Minimist , cmdArgs ;SmsSender = require ( 'sms24x7-client' ) ; Minimist = require
短信发送器 (pbsol.ru) 短信发送包装器( ) 安装 ... 您必须创建solid-cert.crt : cat cert.crt cert.key > solid-cert.crt 。... 'smsSender' => array( 'class' => 'ext.PbsolSmsSender.PbsolS
twiliosms_wampservice Crossbar WAMP路由器的Twilio SMS服务 在安装了Crossbar路由器的Python环境中安装...- classname : twiliosms_wampservice.SMSSender id : Twilio SMS Sender realm : realm1 extra : twili
PHP SDK,用于与XML API Mobile Active一起使用要使用Mobile Active SMS消息服务的XML API,您需要在指定的服务中具有用户名和密码,以接收用户名...$ smsSender = new Mobak ([ 'login' => 'YOUR_LOGIN' , 'password'
symfony-sms-bundle 用于支持 SMS 的 Symfony2 包 激活捆绑 在您的 AppKernel 中: new cspoo\SmsBundle\cspooSmsBundle(), 配置示例 # config.yml ... $sms = $smsSender->createSms($phone, $to
class SMSSender { private static String clientNo = " YOUR_CLIENT_NO " ; private static String clientPass = " YOUR_CLIENT_PASS " ; public static void main ( String [] args ) { SMS client = new SMS ...
短信发送smslib,包含文档全部资源,内部包含各种lib包