import java.util.linkedlist;
import org.jsmsengine.cincomingmessage;
import org.jsmsengine.cmessage;
import org.jsmsengine.coutgoingmessage;
import org.jsmsengine.cservice;
@suppresswarnings("unchecked")
/****
* 收发短信的服务类
*/
public class msgservice
{
private static cservice cs = null;
public static void init(string port, integer fm)
{
// 指定端口号和频率
cs = new cservice(port, fm);
}
/**
* 发送短信
*
* @param phone
* 手机号
* @param info
* 短信内容
*/
public static void sendmsg(string phone, string info)
{
try
{
system.out.println("正在发送短信");
// 初始化连接
cs.initialize();
// 设置jsmsengine服务的缓存
cs.setcachedir(".\\");
// 连接
int status = cs.connect();
// 连接成功(可以是gsm modem、带有红外线、蓝牙、数据线的手机)
if (status == cservice.err_ok)
{
cs.setoperationmode(cservice.mode_pdu);
cs.setsmscnumber("");
coutgoingmessage msg = new coutgoingmessage(phone, info);
msg.setmessageencoding(cmessage.message_encoding_unicode);
if (cs.sendmessage(msg) == cservice.err_ok)
{
system.out.println("给" phone "发送短信成功!");
} else
{
system.out.println("给" phone "发送短信失败!");
}
// 断开连接
cs.disconnect();
} else
{
system.out.println("连接设备失败, 异常: " status);
}
} catch (exception e)
{
e.printstacktrace();
}
}
/***
* 读取短信内容
*/
public static void readmsg()
{
//存入短信集合
linkedlist msglist = new linkedlist();
try
{
system.out.println("读取短信中。。。。");
cs.initialize();
cs.setcachedir(".\\");
// 连接设备
int status = cs.connect();
if (status == cservice.err_ok)
{
cs.setoperationmode(cservice.mode_pdu);
if (cs.readmessages(msglist, cincomingmessage.class_all) == cservice.err_ok)
for (int i = 0; i < msglist.size(); i)
{
cincomingmessage msg = (cincomingmessage) msglist.get(i);
system.out.println(msg);
//输出短信文本
system.out.println(msg.gettext());
//删除短信
cs.deletemessage(i);
}
// 断开连接
cs.disconnect();
} else
system.out.println("设备连接失败:" status);
} catch (exception e)
{
e.printstacktrace();
}
}
public static void main(string[] args)
{
init("com4", 9600);
sendmsg("15800000000", "这是一个测试程序");
readmsg();
}
}