java sms_如何使用Java发送SMS

小编典典

您可以使用此免费的Java示例程序使用连接到计算机的GSM调制解调器将PC上的SMS发送到COM端口。您还需要从Sun下载并安装Java comm api。

该程序需要以下Java文件才能运行。

SerialConnection.java(此文件用于从Java程序连接到COM端口)

SerialConnectionException.java(此文件用于处理Java程序中的串行连接异常)

SerialParameters.java(此程序用于设置COM端口属性,以从Java程序连接到COM端口)

Sender.java(这是实现可运行并使用串行连接发送SMS的程序)

SMSClient.java(此Java类是可以在您自己的java程序中实例化并调用以发送SMS的主类。该程序将在内部使用上述所有四个文件来发送SMS)。

下载发送SMS Java示例程序文件

/*

*

* A free Java sample program

* A list of java programs to send SMS using your COM serial connection

* and a GSM modem

*

* @author William Alexander

* free for use as long as this comment is included

* in the program as it is

*

* More Free Java programs available for download

* at http://www.java-samples.com

*

*

* Note: to use this program you need to download all the 5 java files

* mentioned on top

*

*/

public class SMSClient implements Runnable{

public final static int SYNCHRONOUS=0;

public final static int ASYNCHRONOUS=1;

private Thread myThread=null;

private int mode=-1;

private String recipient=null;

private String message=null;

public int status=-1;

public long messageNo=-1;

public SMSClient(int mode) {

this.mode=mode;

}

public int sendMessage (String recipient, String message){

this.recipient=recipient;

this.message=message;

//System.out.println("recipient: " + recipient + " message: " + message);

myThread = new Thread(this);

myThread.start();

// run();

return status;

}

public void run(){

Sender aSender = new Sender(recipient,message);

try{

//send message

aSender.send ();

// System.out.println("sending ... ");

//in SYNCHRONOUS mode wait for return : 0 for OK,

//-2 for timeout, -1 for other errors

if (mode==SYNCHRONOUS) {

while (aSender.status == -1){

myThread.sleep (1000);

}

}

if (aSender.status == 0) messageNo=aSender.messageNo ;

}catch (Exception e){

e.printStackTrace();

}

this.status=aSender.status ;

aSender=null;

}

}

2020-10-20

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值