接收包含特定代码或文本的短信并发送网络请求
我在这里需要一些帮助,之前已经做过研究,但仍然无法得到任何确切的解决方案。
我这里有这段代码。
可以接收消息,但我希望我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在寻找什么样的文本模式?您可以仅使用 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 useHttpPost
class.