⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multipartmessage.java

📁 控制手机发送短信程序
💻 JAVA
字号:
package sms;

import java.io.File;
import java.io.IOException;

import com.objectxp.msg.*;


/**
 * This class demonstrates how to send multipart (concatenated)
 * messages using jSMS.
 *
 * Have a look at the SendLongText example, to see how to send concatenated
 * messages without having to deal with constructing the proper SMS User
 * Data Header.
 *
 **/
public class MultiPartMessage
{
  private static String longText =
    "This facility allows short messages to be concatenated to form a longer "+
    "message. \nIn the case of uncompressed 8-bit data, the maximum length of "+
    "the short message within the TP-UD field is 134 (140-6) octets. \nIn the "+
    "case of compressed GSM Default alphabet 7 bit data, 8 bit data or UCS2 "+
    "the maximum length of the compressed short message within the TP-UD field "+
    "is 134 (140-6) octets including the Compression Header and Compression "+
    "Footer, both or either of which may be present (See subclause 3.9).";

  private SmsService service;
  private int msgReference=1;

 public static void main(String args[]) throws IOException, MessageException
  {
    if(args.length != 2 ) {
      System.err.println("Usage: MultiPartMessage <config-file> <recipient>");
      System.exit(1);
    }

    File configFile = new File(args[0]);
    MultiPartMessage msg = new MultiPartMessage(configFile);
    msg.sendMessage(args[1], longText);
  }

  public MultiPartMessage(File configFile)
    throws IOException, MessageException
  {
    service = new GsmSmsService();
    service.init(configFile);
  }

  private void sendMessage(String recipient, String txt)
    throws MessageException
  {
    if( txt == null || recipient == null ) return;
    if( txt.length() <= 160 ) {
      SmsMessage msg = new SmsMessage();
      msg.setRecipient(recipient);
      msg.setMessage(txt);
      try {
        service.connect();
        service.sendMessage(msg);
        service.disconnect();
      } finally {
        service.destroy();
      }
      return;
    }

    // calculate number of chunks required for the message (Max 7-bit text
    // length for multipart-messages according to GSM03.40 is 153).
    int chunks = txt.length()/153+1;

    // Construct the header
    byte[] header = new byte[6];
    header[0]=5; // length of header
    header[1]=0; // element id: concatenated short messages, 8bit reference number
    header[2]=3; // Length of element
    header[3]=(byte) ((msgReference++)%256);
    header[4]=(byte)chunks; // Number of parts


    try {
      service.connect();
      for( int i=1; i<=chunks; i++ ) {
        String tc = txt.substring((i-1)*153);
        if( i != chunks ) {
          tc = tc.substring(0,153);
        }
        SmsMessage msg = new SmsMessage();
        msg.setMessage(tc);
        msg.setRecipient(recipient);
        header[5]=(byte)i;
        msg.setUserDataHeader(header);
        service.sendMessage(msg);
      }
      service.disconnect();
    } finally {
      service.destroy();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -