1

我尝试了这个,但它有时无法正常工作..我使用了一个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");

        }
4

1 回答 1

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
}

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

希望你会发现这很有用。

于 2016-12-29T17:24:24.703 回答