I have an app to detect incoming SMS using BroadcastReceiver. It works good for short SMS but not working for multipart SMS i.e. SMS which has more than 160 characters. If I use the code for "multipart" as shown in this link, the remaining part of the code doesn't work in which I'm sending the message to my email address. I get 2 emails: (1) with sender's name and full message and (2) sender's number and full message. It should not send the second email as the sender is saved in my contacts which I'm checking in my code.
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
}