我正在开发一个Android应用程序,当它到来时使用广播接收器获取SMS。这是代码:
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs;
if (bundle != null) {
//---retrieve the SMS message received---
try {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
String address = msgs[i].getOriginatingAddress();
String body = msgs[i].getMessageBody();
}
} catch(Exception e) {
Log.d("Exception caught",e.getMessage());
}
}
}
问题是我收到的消息很少有两三个部分。我不知道如何加入这些部分的消息。如何检测消息的第一部分需要与下一部分消息组合。
第一部分:Your airtel mobile ********** online recharge txn ID ************ o
第二部分:f Rs *** has been initiated. Please keep the txn id for future refe
第三部分:rence.
答案 0 :(得分:1)
您需要添加邮件正文(部分)。 改变这个
String body += msgs[i].getMessageBody();
到
#include <stdlib.h>
#include <stdio.h>
#include "RanNum.h"
int main()
{
//The arbitrary number to be looked for
int iNum = 27;
int iFirst = 0;
int iTotal = 0;
int aRanArray[1000];
//Generating the random aRanArray
for(int i = 0; i <= 1000; i++)
{
//Fills array
aRanArray[i] = rand() % 1000;
//Prints out numbers within array
printf("%d ", aRanArray[i]);
}
//Finding the first time iNum appears in aRanArray
iFirst = First(aRanArray, iNum);
//Finding the number of times iNum appears in aRanArray
iTotal = Total(aRanArray, iNum);
//Printing out the values
printf ("%d", iFirst);
printf ("%d", iTotal);
}
答案 1 :(得分:0)
问题出在你的循环中试试这个,你应该没问题
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
// Retrieve the SMS Messages received
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
// For every SMS message received
for (int i=0; i < msgs.length; i++) {
// Convert Object array
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
// Sender's phone number
str += "SMS from " + msgs[i].getOriginatingAddress() + " : ";
// Fetch the text message
str += msgs[i].getMessageBody().toString();
str += "\n";
}
// Display the entire SMS Message
Log.d(TAG, str);
}