接收包含特定代码或文本的短信并发送网络请求

发布于 2024-12-21 12:44:41 字数 1657 浏览 0 评论 0原文

我在这里需要一些帮助,之前已经做过研究,但仍然无法得到任何确切的解决方案。
我这里有这段代码。
可以接收消息,但我希望我的 Android 能够侦听包含某些代码/文本的特定短信。如果检测到代码/文本,它将向包含 servlet 的 Java Web 应用程序发送 Web 请求。有关如何修改它或解决方案的任何帮助吗?
使用:Eclipse Indigo 2.7,API 2.3.3
任何帮助将不胜感激。
谢谢。

@SuppressWarnings("deprecation")
public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras(); 
        [] msgs = null;
        String str = ""; 
        if (bundle != null) {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");


            msgs = new [pdus.length]; 
            for (int i=0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                str += "SMS from " + msgs[i].getOriginatingAddress();
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";
            }

            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }
    }
}

我做了一个正则表达式示例(如下),但我对将其实现到上面的代码中感到困惑=/

public static void main(String[] args) {

String number = "123345";

//Direct use of Pattern: 
Pattern p = Pattern.compile("\\d+"); 
Matcher m = p.matcher(number); 

while (m.find()) { // Find each match in turn; String can't do this.  
    String result = m.group(); // Access a submatch group; String can't do this. 
    System.out.println("Result: "+result);
    }
}

}

I need some help here, have done research before but still can't get any exact solution.
I have this chunk of codes here.
Can receive message but I want my android to listens for specific SMS messages that contain certain codes/text. and if the code/text is being detected, it will send web request to java web application that contains servlet. any help on how to modify it or solutions?
Using: Eclipse Indigo 2.7, API 2.3.3
Any help would be highly appreciated.
Thank you.

@SuppressWarnings("deprecation")
public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras(); 
        [] msgs = null;
        String str = ""; 
        if (bundle != null) {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");


            msgs = new [pdus.length]; 
            for (int i=0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                str += "SMS from " + msgs[i].getOriginatingAddress();
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";
            }

            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }
    }
}

I did a regex example (as below) but I'm confused about implementing it into the above codes =/

public static void main(String[] args) {

String number = "123345";

//Direct use of Pattern: 
Pattern p = Pattern.compile("\\d+"); 
Matcher m = p.matcher(number); 

while (m.find()) { // Find each match in turn; String can't do this.  
    String result = m.group(); // Access a submatch group; String can't do this. 
    System.out.println("Result: "+result);
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无边思念无边月 2024-12-28 12:44:41

您在寻找什么样的文本模式?您可以仅使用 String.contains() 来检测特殊文本。对于发送,您可以使用 HttpPost 类。

What kind of pattern in text are you looking for? You can just use String.contains() for detecting special text. For sending you can use HttpPost class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文