使用java smslib库未收到SMS

时间:2016-12-27 10:40:37

标签: java smslib

我尝试使用它,但有时候无法正常工作..我使用了一个while循环来循环代码。我可以为此添加一些列表器吗?任何人都可以给我正确答案吗?需要实时回应

 while (true) {
            msgList = new ArrayList<InboundMessage>();
            Service.getInstance().readMessages(msgList, InboundMessage.MessageClasses.ALL);
            for (InboundMessage im : msgList) {

                if (last < im.getMemIndex()) {
                    ResultSet rs = DB.getConnection().createStatement().executeQuery("Select * From codes where code='" + im.getText() + "'");
                    if (rs.next()) {
                        ResultSet rs2 = DB.getConnection().createStatement().executeQuery("Select * From sms_log where code='" + im.getText() + "' AND tel_no='" + im.getOriginator() + "'");
                        if (rs2.next()) {
                                if (m == null) {
                                    m = new SMSClient(1);
                                }
                                m.sendMessage(im.getOriginator(), "The Code is Already Sent... Thank You!.");

                            System.out.println("The Code is Already Sent... Thank You!.");
                        } else {
                            System.out.println("The Code Verified... Thank You!.");
                            if (m == null) {
                                m = new SMSClient(1);
                            }

                            m.sendMessage(im.getOriginator(), "The Code Verified... Thank You!.");
                            DB.getConnection().createStatement().execute("INSERT INTO sms_log (tel_no,code,status) values('" + im.getOriginator() + "','" + im.getText() + "',1)");

                        }
                    } else {
                        if (m == null) {
                            m = new SMSClient(1);
                        }
                        m.sendMessage(im.getOriginator(), "Invalid Code... Thank You!.");
                        System.out.println("Invalid Code... Thank You!.");
                    }

                }
            }
            Thread.sleep(10000);
            System.out.println("start");

        }

1 个答案:

答案 0 :(得分:0)

我认为IInboundMessageNotification是您正在寻找的界面

public class InboundNotification implements IInboundMessageNotification {

    @Override
    public void process(AGateway aGateway, Message.MessageTypes messageTypes, InboundMessage inboundMessage) {
    //add you logic for received messages here 
    }
}

将通知类添加到smsLib服务

Service.getInstance().setInboundMessageNotification(new InboundNotification())

从现在开始,每次调制解调器收到消息时都会调用 process() 方法。

据我记忆,smslib(版本3.5.x)不会删除收到的邮件,因此需要手动完成

@Override
public void process(AGateway aGateway, Message.MessageTypes messageTypes, InboundMessage inboundMessage) {
   try {
         aGateway.deleteMessage(inboundMessage);
       } catch (TimeoutException | GatewayException | InterruptedException | IOException e) {
            e.printStackTrace();
       }
   // your logic here
}

否则,每次收到新消息时,您都会继续收到未删除的消息。

希望你会发现这很有用。