简单的手机发短消息工具类:SMSSender.java

SMSSender.java是一个用于发送短信的独立线程工具类。通过调用getSMSSender获取实例,设置地址和内容后启动线程即可发送。在发送过程中,如果成功会设置doSuccessfully为true,出现错误则设置errorOccured为true。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用法:
String add=address.getString();
String con=content.getString();
SMSSender smsSender = SMSSender.getSMSSender();
smsSender.setMessageText(add,con);
Thread t = new Thread(smsSender);
t.start();


import java.io.IOException;

import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

/**
 * File Name  : SMSSender.java
 * Created on : 2005-4-28
 * Summary	  : A standalone thead, which is used to send SMS.
 * Author	  : Jedi Chen
 */

public final class SMSSender implements Runnable
{   

  // the string stores the text message that will be sent.
  private String message;
  // the destination address for current SMS sender.
  private String address;

  // boolean variable indicates whether the SMS has been sent successfully.
  // initialized as false when get the singleton instance.
  private static boolean doSuccessfully;

  // boolean variable indicates whether error occured while sending SMS.
  // initialized as false when get the singleton instance.
  private static boolean errorOccured;

  // the singleton instance of SMSSender, since one instance is enough
  // for one MIDlet, we apply the Singleton pattern for this class.
  private static SMSSender instance;

  /**
   * the Factory method to get the singleton instance.
   */
  public static SMSSender getSMSSender() {
    if (instance == null) {
    	instance = new SMSSender();
    } else {
    	instance.reset();
    }

    return instance;
  }

  /**
   * The private constructor for SMSSender, only could be called
   * by getSMSSender.
   *
   * call resetSenderStatus() to reset the members.
   */
  private SMSSender() {
    reset();
  }
  /**
   * Once the caller get the sender status, it must
   * call this method to reset both status.
   */
  private void reset() {
	doSuccessfully = false;
	errorOccured = false;
    address = null;
    message = null;
  }

  public synchronized void setMessageText(String address, String message) {

    // assert(address != null && !address.equals(""));
    this.address = "sms://" + address;

    if(message == null || message.equals(""))
    	message = "[WARN] Error formatted message!";
    
    this.message = message;
//    System.out.println("[SMS] " + s);
//    m_fDoSuccessfully = false;
//    m_fErrorOccured = false;
  }

  /*
   * Send the message in a standalone thread.
   *
   * @see java.lang.Runnable#run()
   */
  public void run() {
    MessageConnection smsconn = null;
    try {
      smsconn = (MessageConnection) Connector.open(address);

      TextMessage txtmsg = (TextMessage) smsconn.newMessage(MessageConnection.TEXT_MESSAGE);
      txtmsg.setPayloadText(message);
      
      smsconn.send(txtmsg);

      doSuccessfully = true;
//      System.out.println("[SMS] SMS sent successfully :)");
    } catch (Exception expt) {
    	errorOccured = true;
//      System.out.println("[SMS] SMS sent error!");
    } finally {
      if (smsconn != null) {
        try {
          smsconn.close();
        } catch (IOException ioex) {
//          System.out.println("[SMS] Close SMS connection error caught!");
        }
      }
    }
  }

  /**
   * @return Returns the doSuccessfully.
   */
  public static synchronized boolean isDoSuccessfully() {
    return doSuccessfully;
  }
  /**
   * @return Returns the errorOccured.
   */
  public static synchronized boolean isErrorOccured() {
    return errorOccured;
  }
  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值