📄 smshelper.java
字号:
package edu.soft.buaa.message.sms;
import java.util.*;
public class SMSHelper {
private static SMSHelper helper = null;
private static Object initLock = new Object();
private static Object initLock2 = new Object();
private SMSService srv = null;
private String comport = "com1";
private int baud = 9600;
private int MessageMode = SMSService.MODE_PDU;
private String SMSCenterNo = "+8613800100500";
private int semaphore = 1;
public void P() {
synchronized(initLock){
semaphore--;
while(semaphore < 0) {
try{
Thread.sleep(5);
}catch(Exception exs){
}
}
}
}
public void V() {
synchronized(initLock2){
semaphore++;
}
}
private SMSHelper() {
}
private void init() {
if (srv == null)
srv = new SMSService(comport, baud);
}
public void setSMSCenterNo(String no) {
if (!no.startsWith("+86")) {
no = "+86" + no;
}
this.SMSCenterNo = no;
}
public void setPort(String port) {
comport = port;
}
public void setBaud(int baud) {
this.baud = baud;
}
public LinkedList readMessage() {
int status;
init();
LinkedList ll = new LinkedList();
LinkedList returnlist = new LinkedList();
try {
// Initialize service.
srv.initialize();
// Set the cache directory.
srv.setCacheDir(".\\");
// Connect to GSM device.
status = srv.connect();
// Did we connect ok?
if (status == SMSService.ERR_OK) {
// Set the operation mode to PDU
srv.setOperationMode(SMSService.MODE_PDU);
// Request to read all messages.
if (srv.readMessages(ll, IncomingMessage.CLASS_ALL)
== SMSService.ERR_OK)
// Enumerate and print all messages.
for (int i = 0; i < ll.size(); i++) {
IncomingMessage msg = (IncomingMessage) ll.get(i);
//System.out.println(msg);
ShortMessage sm =
new ShortMessage(
msg.getDate(),
msg.getOriginator(),
msg.getRecipient(),
msg.getText(),
msg.getMemIndex());
returnlist.add(sm);
}
// Enough for now...
// Disconnect from GSM device.
srv.disconnect();
} else
System.out.println(
"Connection to mobile failed, error: " + status);
} catch (Exception e) {
e.printStackTrace();
}
return returnlist;
}
public LinkedList receiveMessage() {
int status;
init();
LinkedList ll = new LinkedList();
LinkedList returnlist = new LinkedList();
try {
// Initialize service.
srv.initialize();
// Set the cache directory.
srv.setCacheDir(".\\");
// Connect to GSM device.
status = srv.connect();
// Did we connect ok?
if (status == SMSService.ERR_OK) {
// Set the operation mode to PDU
srv.setOperationMode(SMSService.MODE_PDU);
// Request to read all messages.
if (srv.readMessages(ll, IncomingMessage.CLASS_ALL)
== SMSService.ERR_OK)
// Enumerate and print all messages.
for (int i = 0; i < ll.size(); i++) {
IncomingMessage msg = (IncomingMessage) ll.get(i);
//System.out.println(msg);
ShortMessage sm =
new ShortMessage(
msg.getDate(),
msg.getOriginator(),
msg.getRecipient(),
msg.getText(),
msg.getMemIndex());
srv.deleteMessage(msg.getMemIndex());
returnlist.add(sm);
}
// Enough for now...
// Disconnect from GSM device.
srv.disconnect();
} else
System.out.println(
"Connection to mobile failed, error: " + status);
} catch (Exception e) {
e.printStackTrace();
}
return returnlist;
}
public LinkedList readOldMessage() {
int status;
init();
LinkedList ll = new LinkedList();
LinkedList returnlist = new LinkedList();
try {
// Initialize service.
srv.initialize();
// Set the cache directory.
srv.setCacheDir(".\\");
// Connect to GSM device.
status = srv.connect();
// Did we connect ok?
if (status == SMSService.ERR_OK) {
// Set the operation mode to PDU
srv.setOperationMode(SMSService.MODE_PDU);
// Request to read all messages.
if (srv.readMessages(ll, IncomingMessage.CLASS_ALL)
== SMSService.ERR_OK)
// Enumerate and print all messages.
for (int i = 0; i < ll.size(); i++) {
IncomingMessage msg = (IncomingMessage) ll.get(i);
//System.out.println(msg);
ShortMessage sm =
new ShortMessage(
msg.getDate(),
msg.getOriginator(),
msg.getRecipient(),
msg.getText(),
msg.getMemIndex());
returnlist.add(sm);
}
// Enough for now...
// Disconnect from GSM device.
srv.disconnect();
} else
System.out.println(
"Connection to mobile failed, error: " + status);
} catch (Exception e) {
e.printStackTrace();
}
return returnlist;
}
public LinkedList readNewMessage() {
int status;
init();
LinkedList ll = new LinkedList();
LinkedList returnlist = new LinkedList();
try {
// Initialize service.
srv.initialize();
// Set the cache directory.
srv.setCacheDir(".\\");
// Connect to GSM device.
status = srv.connect();
// Did we connect ok?
if (status == SMSService.ERR_OK) {
// Set the operation mode to PDU
srv.setOperationMode(SMSService.MODE_PDU);
// Request to read all messages.
if (srv.readMessages(ll, IncomingMessage.CLASS_REC_UNREAD)
== SMSService.ERR_OK)
// Enumerate and print all messages.
for (int i = 0; i < ll.size(); i++) {
IncomingMessage msg = (IncomingMessage) ll.get(i);
//System.out.println(msg);
ShortMessage sm =
new ShortMessage(
msg.getDate(),
msg.getOriginator(),
msg.getRecipient(),
msg.getText(),
msg.getMemIndex());
returnlist.add(sm);
}
// Enough for now...
// Disconnect from GSM device.
srv.disconnect();
} else
System.out.println(
"Connection to mobile failed, error: " + status);
} catch (Exception e) {
e.printStackTrace();
}
return returnlist;
}
public LinkedList receiveNewMessage() {
int status;
init();
LinkedList ll = new LinkedList();
LinkedList returnlist = new LinkedList();
try {
// Initialize service.
srv.initialize();
// Set the cache directory.
srv.setCacheDir(".\\");
// Connect to GSM device.
status = srv.connect();
// Did we connect ok?
if (status == SMSService.ERR_OK) {
// Set the operation mode to PDU
srv.setOperationMode(SMSService.MODE_PDU);
// Request to read all messages.
if (srv.readMessages(ll, IncomingMessage.CLASS_REC_UNREAD)
== SMSService.ERR_OK)
// Enumerate and print all messages.
for (int i = 0; i < ll.size(); i++) {
IncomingMessage msg = (IncomingMessage) ll.get(i);
//System.out.println(msg);
ShortMessage sm =
new ShortMessage(
msg.getDate(),
msg.getOriginator(),
msg.getRecipient(),
msg.getText(),
msg.getMemIndex());
srv.deleteMessage(msg.getMemIndex());
returnlist.add(sm);
}
// Enough for now...
// Disconnect from GSM device.
srv.disconnect();
} else
System.out.println(
"Connection to mobile failed, error: " + status);
} catch (Exception e) {
e.printStackTrace();
}
return returnlist;
}
public void deleteMessage(int memId) {
int status;
init();
try {
// Initialize service.
srv.initialize();
// Set the cache directory.
srv.setCacheDir(".\\");
// Connect to GSM device.
status = srv.connect();
// Did we connect ok?
if (status == SMSService.ERR_OK) {
// Set the operation mode to PDU
srv.setOperationMode(SMSService.MODE_PDU);
srv.deleteMessage(memId);
// Enough for now...
// Disconnect from GSM device.
srv.disconnect();
} else
System.out.println(
"Connection to mobile failed, error: " + status);
} catch (Exception e) {
e.printStackTrace();
}
}
public void SendMessageList(LinkedList ll) {
int status;
init();
LinkedList tmplist = new LinkedList();
for (int i = 0; i < ll.size(); i++) {
ShortMessage sm = (ShortMessage) ll.get(i);
String phone = sm.getToPhone();
String text = sm.getText();
if (!phone.startsWith("+86")) {
phone = "+86" + phone;
}
OutgoingMessage ogm = new OutgoingMessage(phone, text);
ogm.setMessageEncoding(SMSMessage.MESSAGE_ENCODING_UNICODE);
tmplist.add(ogm);
}
try {
// 初始化机器
srv.initialize();
//// Set the cache directory.
srv.setCacheDir(".\\");
// Set the phonebook.
// srv.setPhoneBook("../misc/phonebook.xml");
// Connect to GSM device.
status = srv.connect();
// Did we connect ok?
if (status == SMSService.ERR_OK) {
// Set the operation mode to PDU - default is ASCII.
srv.setOperationMode(MessageMode);
// Set the SMSC number (set to default).
srv.setSmscNumber(SMSCenterNo);
if (srv.sendMessage(tmplist) == SMSService.ERR_OK)
System.out.println("Message Sent Success!");
else
System.out.println("Message Failed!");
// Disconnect from GSM device.
//srv.disconnect();
} else
System.out.println(
"Connection to mobile failed, error: " + status);
V();
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendMessage(String phone, String text) {
int status;
init();
if (!phone.startsWith("+86")) {
phone = "+86" + phone;
}
try {
// 初始化机器
srv.initialize();
// Set the cache directory.
srv.setCacheDir(".\\");
// Set the phonebook.
// srv.setPhoneBook("../misc/phonebook.xml");
// Connect to GSM device.
status = srv.connect();
// Did we connect ok?
if (status == SMSService.ERR_OK) {
// Set the operation mode to PDU - default is ASCII.
srv.setOperationMode(MessageMode);
// Set the SMSC number (set to default).
srv.setSmscNumber(SMSCenterNo);
// Create a OutgoingMessage object and dispatch it.
// *** Please update the phone number with one of your choice ***
OutgoingMessage msg = new OutgoingMessage(phone, text);
// Character set is 7bit by default - lets make it UNICODE :)
// We can do this, because we are in PDU mode (look at line 63). When in ASCII mode,
// this does not make ANY difference...
msg.setMessageEncoding(SMSMessage.MESSAGE_ENCODING_UNICODE);
if (srv.sendMessage(msg) == SMSService.ERR_OK)
System.out.println(
"Message Sent to " + phone + " Success!");
else
System.out.println("Message Failed!");
// Disconnect from GSM device.
srv.disconnect();
} else
System.out.println(
"Connection to mobile failed, error: " + status);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SMSHelper getInstance() {
if (helper == null) {
synchronized (initLock) {
if (helper == null) {
try {
helper = new SMSHelper();
} catch (Exception ex) {
return null;
}
}
}
}
return helper;
}
public boolean openConnection() {
init();
srv.initialize();
// Set the cache directory.
srv.setCacheDir(".\\");
// Connect to GSM device.
int status = srv.connect();
if (status == SMSService.ERR_OK) {
return true;
} else {
return false;
}
}
public void closeConnection() {
srv.disconnect();
}
public void SendMessageListInThread(LinkedList ll) {
int status;
init();
LinkedList tmplist = new LinkedList();
for (int i = 0; i < ll.size(); i++) {
ShortMessage sm = (ShortMessage) ll.get(i);
String phone = sm.getToPhone();
String text = sm.getText();
if (!phone.startsWith("+86")) {
phone = "+86" + phone;
}
OutgoingMessage ogm = new OutgoingMessage(phone, text);
ogm.setMessageEncoding(SMSMessage.MESSAGE_ENCODING_UNICODE);
tmplist.add(ogm);
}
try {
// Set the SMSC number (set to default).
srv.setSmscNumber(SMSCenterNo);
P();
if (srv.sendMessage(tmplist) == SMSService.ERR_OK)
System.out.println("Message Sent Success!");
else
System.out.println("Message Failed!");
V();
} catch (Exception e) {
e.printStackTrace();
}
}
public LinkedList waitNewMessageInThread() {
int status;
init();
LinkedList ll = new LinkedList();
LinkedList returnlist = new LinkedList();
try {
// Initialize service.
// Did we connect ok?
if (0 == SMSService.ERR_OK) {
// Set the operation mode to PDU
srv.setOperationMode(SMSService.MODE_PDU);
DeadLock : while (!ReceiveSMSThread.CLOSE_SIGNAL) {
Thread.sleep(500);
P();
status = srv.waitMessages(ll);
V();
// Request to read all messages.
if (status == SMSService.ERR_OK) {
// Enumerate and print all messages.
for (int i = 0; i < ll.size(); i++) {
IncomingMessage msg = (IncomingMessage) ll.get(i);
//System.out.println(msg);
ShortMessage sm =
new ShortMessage(
msg.getDate(),
msg.getOriginator(),
msg.getRecipient(),
msg.getText(),
msg.getMemIndex());
P();
srv.deleteMessage(msg.getMemIndex());
V();
returnlist.add(sm);
break DeadLock;
}
} else {
}
}
// Enough for now...
// Disconnect from GSM device.
} else
System.out.println(
"Connection to mobile failed, error: " + status);
} catch (Exception e) {
e.printStackTrace();
}
return returnlist;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -