`

SMSSender

 
阅读更多

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;
}
}

分享到:
评论

相关推荐

    Sms sender

    $smsSender = new SmsSender('服务器URL', 'API密钥'); $smsSender-&gt;connect(); if ($smsSender-&gt;authenticate()) { $smsSender-&gt;sendSMS('目标电话号码', '短信内容'); } else { echo "验证失败"; } ``` 需要...

    .net短信发送实例_短信_

    public SmsSender(string accessKeyId, string accessKeySecret) { // 初始化AccessKey IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret); client = new ...

    SMS_Sender.rar_SMSSender._SMS_Sender_mscomm_sms_sms sender

    自己编写的通过串口发送短信的源代码,使用MSCOMM控件,速度快,稳定!

    COVID19-SMSSender:COVID19-SMSSender是总案例更新的地方,它将向您发送一条短信

    COVID19-SMSSender 只要该选定国家/地区中有更多“总案件”,您就可以实时接收短信。 屏幕截图 要求 Twilio帐户 如何安装 在命令提示符中运行以下命令: pip install -r requirements.txt 根据您的需要编辑config....

    利用Ptrace在Android平台实现应用程序控制

    下面是一个简单的使用Ptrace在Android 2.3上实现控制应用程序示例,目标是阻止SmsSender1应用发送短信: 1. 创建测试程序SmsSender1: - 在SmsSender1的onCreate方法中,程序会获取SmsManager并尝试发送一条短信。...

    ASP.NET短信发送接口以及实例

    var smsSender = new SmsSender("your_api_key_here"); var isSent = smsSender.SendSms(phoneNumber, message); if (isSent) { lblResult.Text = "短信已发送,请查收!"; } else { lblResult.Text = ...

    c#在windowmobile5里使用sms.dll发短信的类源码

    SMSSender sender = new SMSSender(); bool isSent = sender.Send("+123456789", "测试短信内容"); if (isSent) { Console.WriteLine("短信已成功发送!"); } else { Console.WriteLine("短信发送失败。");...

    短信验证demo

    SmsSender smsSender = new SmsSender(); smsSender.send("13800138000", smsObserver); ``` 以上代码展示了如何初始化SDK、创建观察者对象以及发送验证码的基本步骤。实际项目中,还需要根据具体业务需求进行相应...

    调用接口发送短信asp的和c#的例子都有

    SmsSender sender = new SmsSender(); string response = await sender.SendSms("13800138000", "你好,这是测试短信"); // 替换为实际手机号和短信内容 Console.WriteLine(response); } } ``` 在这个C#示例中,...

    飞鸽短信平台 API接口实现 [VB6源代码]

    3. `SMSSender.vbw`:VB6的工作空间文件,记录了项目的所有组件、设置和引用,有助于在开发环境中恢复项目的原始状态。 4. `SMSSender.vbp`:VB6的工程文件,存储了项目的基本信息,如引用的库、使用的模块、工程组...

    c#短信开发c#短信开发

    在C#中,可以创建一个`SmsSender`类,封装上述步骤,提供发送短信的接口。例如: ```csharp public class SmsSender { private SerialPort _serialPort; public SmsSender(string portName, int baudRate) { _...

    JAVA发送手机短信依赖JAR包

    通常,它会包含如`SmsSender`、`Message`等核心类,以及`send`、`sendBatch`等发送短信的方法。 4. **创建短信对象**:根据服务提供商的要求,创建`Message`对象,包含接收方手机号码、短信内容、发送者ID等信息。 ...

    Laravel开发-simple-sms-drivers

    3. **驱动器实现**:包中可能包含了多种短信服务提供商的驱动器类,每个类都实现了`SmsSender`接口,如`TwilioDriver`、`NexmoDriver`等。这些驱动器处理与具体服务商的通信,如构建HTTP请求、发送POST数据、解析...

    设计模式简要说明

    - **示例**:例如,在发送邮件和短信的例子中,我们可以创建一个`Sender`接口,然后定义`MailSender`和`SmsSender`两个实现类。再创建一个`SendFactory`工厂类,根据传入的不同类型参数返回不同的`Sender`实例。 ...

    23种设计模式

    例如,在上面的示例中,我们创建了一个`SendFactory`类,它可以创建不同的发送者对象,如`MailSender`和`SmsSender`。 ```java // 定义一个公共接口 public interface Sender { public void Send(); } // 实现类 ...

    c#在windowmobile5里使用sms.dll发短信

    在上面的代码中,我们创建了一个`SmsSender`类,初始化时会实例化`smsDevice`对象。`SendMessage`方法用于发送单条短信,`SendMultipleMessages`方法用于一次性发送多条短信。`GetDeliveryReport`方法则用于注册短信...

    SendSMS.rar_windows mobile_windows mobile sms_短信 csharp

    public class SmsSender { private SmsService smsService; public SmsSender() { smsService = new SmsService(); if (!smsService.IsAvailable) { throw new Exception("SMS service is not available.");...

    元旦、春节新年祝福短信.zip

    在这个示例中,我们创建了一个`SmsSender`类,它包含了发送短信的方法`SendSms`。这个方法接受电话号码和短信内容作为参数,构造一个JSON格式的请求体,并通过HTTP POST请求发送到短信服务API。 对于新年祝福短信,...

    网建短信包(Java).7z

    接着,他们可以通过创建SmsSender实例,调用其sendSms方法,传入接收方手机号码和短信内容,即可触发短信发送。如果需要更复杂的功能,比如定时发送、批量发送或模板短信,他们可以查阅提供的文档或示例代码来学习...

    Android 中常用设计模式

    public class SmsSender implements Sender { @Override public void send() { System.out.println("This is sms sender!"); } } public class SendFactory { public Sender produce(String type) { if (...

Global site tag (gtag.js) - Google Analytics