`

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics