文档中心 > API文档

1.7.3.1.4. 代码示例

以短信王为例:

public class AdapterMobileMessageManagerShortMessageKingImpl implements
     AdapterMobileMessageManger {
     //标识名称
     public String getName() {
         return "短信王短信";
     }
     //单个短信发送
     public boolean sendMessage(int messageId, String srcPhone,
         String destPhone, String content) {
         //短信的内容不超过 60个字,小灵通号不超过 40个字。
         Sender sender = new Sender(kingName, kingPassword);
         String result = sender.massSend(destPhone, content, "", "");
         String success = getParameters(result).get("success");
         if (Strings.isBlank(success)) {
             return false;
         }
         String[] str = success.split(",");
         boolean successSend = false;
         for (int i = 0; i < str.length; i++) {
             if (str[i].equals(destPhone)) {
                 successSend = true;
             }
         }
         return successSend;
     }
     //接收短信
     public List < MobileReciver > recive() {
         Sender sender = new Sender(kingName, kingPassword);
         String str = sender.readSms();
         String string = null;
         String srcphonenum = null;
         String srcPhone = null;
         String content = null;
         if (str.indexOf("&msg=") != str.indexOf("&dst=")) {
             string = str.substring(str.indexOf("&msg"), str.indexOf("&dst"));
             content = string.substring(string.indexOf("=") + 1);
         }
         if (str.indexOf("&src=") != str.indexOf("&msg=")) {
             srcphonenum = str.substring(str.indexOf("&src="), str.indexOf("&msg="));
             if (srcphonenum != null) {
                 srcPhone = srcphonenum.substring(srcphonenum.indexOf("=") + 1);
             }
         }
         List < MobileReciver > list = new ArrayList < MobileReciver > ();
         if (content != null && srcPhone != null) {
             MobileReciver reciver = new MobileReciver();
             reciver.setContent(content);
             reciver.setSrcPhone(srcPhone);
             list.add(reciver);
         }
         return list;
     }
 }