我有一个应用程序来检测使用BroadcastReceiver传入的短信。它适用于短SMS但不适用于多部分SMS,即具有超过160个字符的SMS。如果我将代码用于" multipart"如此link所示,代码的其余部分不起作用,我将邮件发送到我的电子邮件地址。我收到2封电子邮件:(1)发件人的姓名和完整邮件以及(2)发件人的号码和完整邮件。它不应该发送第二封电子邮件,因为发件人已保存在我的联系人中,我在我的代码中检查。
public class INSMS extends BroadcastReceiver{
Context mContext;
String msg_body = "";
String mob_no = "";
String dttm;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
msg_body += msgs[i].getMessageBody().toString();
mob_no = msgs[i].getOriginatingAddress();
Calendar calendar = Calendar.getInstance();
Date finaldate = calendar.getTime();
dttm = finaldate.toString(); //retrieve date and time
//Resolving the contact name from the contacts.
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(mob_no));
Cursor c = context.getContentResolver().query(lookupUri, new String[]{ContactsContract.Data.DISPLAY_NAME},null,null,null);
try {
if(c.moveToFirst()){
mob_no = c.getString(0);
mob_no+= ":" + Build.MODEL;
/*Toast.makeText(context, "Name:" + "\t" + mob_no + "\n" +
"Date/Time" + "\t" + dttm + "\n" +
"Message:" + "\t" + msg_body,
Toast.LENGTH_LONG).show(); */
new AsyncCallSMS().execute();
}else{
mob_no+= ":" + Build.MODEL;
/*Toast.makeText(context, "Mobile No:" + "\t" + mob_no + "\n" +
"Date/Time" + "\t" + dttm + "\n" +
"Message:" + "\t" + msg_body,
Toast.LENGTH_LONG).show(); */
new AsyncCallSMS().execute();
}
} catch (Exception e) {
// TODO: handle exception
}finally{
c.close();
}
//new AsyncCallSMS().execute();
}
}
}
private class AsyncCallSMS extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
String webResponse="";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://example.com/log.asmx/broadcast?");
//Post Data
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(5);
nameValuePair.add(new BasicNameValuePair("code", "A1B2C3"));
nameValuePair.add(new BasicNameValuePair("type", "IN"));
nameValuePair.add(new BasicNameValuePair("sender", mob_no));
nameValuePair.add(new BasicNameValuePair("timeStamp", dttm));
nameValuePair.add(new BasicNameValuePair("mess", msg_body));
//Encoding POST data
try{
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
System.out.println(e);
}
//making POST request.
try{
HttpResponse response = httpClient.execute(httpPost);
String XmlString = EntityUtils.toString(response.getEntity());
XmlString=XmlString.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
XmlString = XmlString.substring(XmlString.indexOf("[") + 1, XmlString.lastIndexOf("]"));
JSONObject jObj = new JSONObject(XmlString);
webResponse = jObj.getString("status");
}catch (ClientProtocolException e) {
// Log exception
e.printStackTrace();
System.out.println(e);
} catch (IOException e) {
// Log exception
e.printStackTrace();
System.out.println(e);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return webResponse;
}
@Override
protected void onPostExecute(String webResponse) {
//Toast.makeText(mContext, webResponse, Toast.LENGTH_LONG).show();
System.out.println(webResponse);
}
} // AsyncTask ends
}
&#13;