长短信处理方案
上次写过对短信发送的处理,并没有体现出对长短信的处理方案,在实际应用中发现了以下问题,对于短信长度超过指定的大小时会出现截短的现象.
原信息地址:(里面有源码,和相关的配置),主要对以下方法进行修改
http://blog.csdn.net/fjfdszj/archive/2008/12/18/3549811.aspx
1)这种方案是处理长短信的方式(可以实现向客户端发送的连续的多条短信合并成一条在客户端的手机上显示)
/**
* 华为短信发送类处理(公用类)
*
* @param 短信类,包括手机号,内容,序号
*/
public int SendMessage(SmsBean sms) {
// 当短信收发接口为空时重新初始化
if (myProxy == null) {
System.out.println("myProxy==null");
ProBaseConf();
System.out.println("myProxy信息:" + myProxy.toString());
}
// System.out.println("myProxy.getConnState():"+myProxy.getConnState().toString());
// 返回结果
int result = 0;
// 发送号码
ArrayList telList = sms.getMobileNoList();
dest_Terminal_Id = new String[telList.size()];
for (int i = 0; i < telList.size(); i++) {
dest_Terminal_Id[i] = telList.get(i).toString();
}
// 存活有效期
valid_Time = new Date(System.currentTimeMillis() + (long) 0xa4cb800); // new
// Date();//
// 定时发送时间
at_Time = null;// new Date(System.currentTimeMillis() + (long)
// 0xa4cb800); //new Date();
// 用户手机上显示为短消息的主叫号码
// src_Terminal_Id=src_Terminal_Id+"001";
//封装回执短信处理机制
String src_TerminalTmp=src_Terminal_Id;
if(sms.getSn().length()==8){
src_TerminalTmp=src_TerminalTmp+sms.getSn();//组装原发送号
}
try {
byte[]messageUCS2;
messageUCS2 = sms.getSmsContent().getBytes("UnicodeBigUnmarked");
int messageUCS2Len = messageUCS2.length;
//长短信长度
int maxMessageLen = 120;
//长短信发送
tp_Udhi= 1;
msg_Fmt = 0x08;
int messageUCS2Count=0;
if(messageUCS2Len % (maxMessageLen - 6)==0)
messageUCS2Count = messageUCS2Len / (maxMessageLen - 6);
else
messageUCS2Count = messageUCS2Len / (maxMessageLen - 6) + 1;
//长短信分为多少条发送
byte[]tp_udhiHead = new byte[6];
tp_udhiHead[0] = 0x05;
tp_udhiHead[1] = 0x00;
tp_udhiHead[2] = 0x03;
tp_udhiHead[3] = 0x0A;
tp_udhiHead[4] = (byte)messageUCS2Count;
tp_udhiHead[5] = 0x01;
pk_Total=messageUCS2Count;
//默认为第一条
for (int i = 0; i < messageUCS2Count; i ++ )
{
tp_udhiHead[5] = (byte)(i + 1);
pk_Number=i+1;
if (i != messageUCS2Count - 1)
{
//不为最后一条
msg_Content = byteAdd(tp_udhiHead, messageUCS2, i * (maxMessageLen - 6), (i + 1) * (maxMessageLen - 6));
}
else
{
msg_Content = byteAdd(tp_udhiHead, messageUCS2, i * (maxMessageLen - 6), messageUCS2Len);
}
// 初始化提交信息
CMPPSubmitMessage submitMsg = new CMPPSubmitMessage(pk_Total,
pk_Number, registered_Delivery, msg_Level, service_Id,
fee_UserType, fee_Terminal_Id, tp_Pid, tp_Udhi,
msg_Fmt, msg_Src, fee_Type, fee_Code, valid_Time,
at_Time, src_TerminalTmp, dest_Terminal_Id,
msg_Content, reserve);
CMPPSubmitRepMessage submitRepMsg = (CMPPSubmitRepMessage) myProxy.send(submitMsg);
if (submitRepMsg!= null) {
result = 1;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return result;
}
return result;
}
2)长短信拆分发送显示前缀方案:(这种方案首先保证数据的完整性,同时有时序性)
/**
* 华为短信发送类处理(公用类)
*
* @param 短信类,包括手机号,内容,序号
*/
public int SendMessage(SmsBean sms) {
// 当短信收发接口为空时重新初始化
if (myProxy == null) {
System.out.println("myProxy==null");
ProBaseConf();
System.out.println("myProxy信息:" + myProxy.toString());
}
// System.out.println("myProxy.getConnState():"+myProxy.getConnState().toString());
// 返回结果
int result = 0;
// 发送号码
ArrayList telList = sms.getMobileNoList();
dest_Terminal_Id = new String[telList.size()];
for (int i = 0; i < telList.size(); i++) {
dest_Terminal_Id[i] = telList.get(i).toString();
}
// 存活有效期
valid_Time = new Date(System.currentTimeMillis() + (long) 0xa4cb800); // new
// Date();//
// 定时发送时间
at_Time = null;// new Date(System.currentTimeMillis() + (long)
// 0xa4cb800); //new Date();
// 用户手机上显示为短消息的主叫号码
// src_Terminal_Id=src_Terminal_Id+"001";
//封装回执短信处理机制
String src_TerminalTmp=src_Terminal_Id;
if(sms.getSn().length()==6){
src_TerminalTmp=src_TerminalTmp+sms.getSn();//组装原发送号
}
try {
//msg_Content分隔
// 发送内容
String smscontent = sms.getSmsContent().toString();
int len=smscontent.length();//字符长度
int pagesize=59; //每页大小138~139,对应89
//double page=Math.ceil((double)len/(double)pagesize); //页数
int page=0;
if(len % pagesize>0)
{
page=len/pagesize+1;
}else
page=len/pagesize;
//长短信发送方法
pk_Total=page;
msg_Fmt=15;
for (int m = 1; m <= page; m++) {
msg_Content=null;
try {
if (m==page) {
msg_Content = ("("+m + "/" + page+")" + smscontent.substring(0)).getBytes("GBK");//UTF-16BE ,GBK
} else {
msg_Content = ("("+m + "/" + page +")"+ smscontent.substring(0,pagesize)).getBytes("GBK");//UTF-16BE, GBK
smscontent = smscontent.substring(pagesize).intern();// 页面截取为下一轮准备
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//长短信发送方法
pk_Number=m;
// 初始化提交信息
CMPPSubmitMessage submitMsg = new CMPPSubmitMessage(pk_Total,
pk_Number, registered_Delivery, msg_Level, service_Id,
fee_UserType, fee_Terminal_Id, tp_Pid, tp_Udhi,
msg_Fmt, msg_Src, fee_Type, fee_Code, valid_Time,
at_Time, src_TerminalTmp, dest_Terminal_Id,
msg_Content, reserve);
CMPPSubmitRepMessage submitRepMsg = (CMPPSubmitRepMessage) myProxy.send(submitMsg);
if (submitRepMsg!= null) {
result = 1;
}
/*
if (submitRepMsg.getResult() == 0) {
result = 1;
}
* */
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return result;
}
return result;
}
3.比较,总结
方案一:将客户需要接收的一条长短信内容整合成一条信息,比较容易对一件事情进行处理.
方案二:可以支持那些不支持接收长短信的手机.这种交互方式会比较好.
现在使用的系统采用的是方案一,最初采用的是方案二,但客户提出处理的时候需要对短信进行合并处理,这样可能会比较好的节省流量.