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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值