How to read sms response line by line - android

This is my code to read sms. I want to read received sms line by line if line one contain no 00 do this if line 2 contain this String do this etc.
Below is my code which only read smss containing 00 then do this not read line by line how I change this code to read sms line by line
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString().split("/n");;
str += "\n";
if(str.contains("00"));
{
Intent l = new Intent(context,AgAppMenu.class);
l.putExtra("msg",str);
l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(l);
}

Use String.split().
For example to get the different lines from a String you do:
String example = "Hello\nWorld!";
String[] parts = example.split("\n");
// parts[0] contains "Hello"
// parts[1] contains "World!"
For your case, to get each line of the message, use something like :
String[] msg_lines = msgs[i].getMessageBody().toString().split("/n");
So your code may look like:
String[] msg_lines = null;
for(...) {
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
msg_lines = msgs[i].getMessageBody().toString().split("\n");
for (String line : msg_lines)
{
if (line.contains("00")) { /*then*/ }
}
}

Related

SMS receiving code works on one device but not on other device (Micromax E4820)

onReceive method doesn't gets called.
I checked the broadcast receiver code for calls and it works fine.
The following code works on my Intex Elyt Dual (7.0) but don't work on other with 6.0.1.
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "on Receive", Toast.LENGTH_SHORT).show();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; ++i) {
// Convert Object array
hereComesNewSMS = hereComesNewSMS++ ;
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";
latestSMSnumber = msgs[i].getOriginatingAddress();
latestSMScontent = str;
}
// Display the entire SMS Message
Log.e("TAG1 number: ", latestSMSnumber);
Log.e("TAG2 content: ", str);
}

Android Inbox SMS receiving in different parts

I am developing an android app which gets the SMS using broadcast receiver when its comes. Here is the code:
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());
}
}
}
Problem is that few messages I receive comes in two or three parts. I don't know how to join these parts of messages. How can I detect that the first part of message needed to be combined with the next part message.
First Part: Your airtel mobile ********** online recharge txn ID ************ o
Second Part: f Rs *** has been initiated. Please keep the txn id for future refe
Third Part: rence.
You need to add message body(parts).
change this
String body = msgs[i].getMessageBody();
to
String body += msgs[i].getMessageBody();
The problem is inside your loop try this out, you should be fine
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);
}

received sms from special number go to my program and other received sms go to inbox of phone

I wrote send and receive program in android. when sms received from special number, sms go to my program and body of sms , show in text. but for received sms from other phone number, sms go to inbox of phone and program isn't opened.
Now in my program , for every received sms, program is opened and body of sms is shown in textview.
if condition didn't work!!!
i put my code, please check my code.
SmsReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class SmsReceiver extends BroadcastReceiver {
public String str = "";
static final String ACTION =
"android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null) {
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 msg_from = msgs[i].getOriginatingAddress();
if(msg_from.equals("+9891--------"))
{
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
}
Intent act = new Intent(context, MainActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.putExtra("message", str);
context.startActivity(act);
}
abortBroadcast();
}
}
}
You should do the context.startActivity(act); in the if condition. I think you mistakenly put it out of the if condition.
Also the abortBroadcast() should be in if condition coz that makes sense, if the msg is not from the special number it should go to the inbox....RIGHT :)
Edited:
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION)) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null) {
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 msg_from = msgs[i].getOriginatingAddress();
if (msg_from.equals("+9891--------")) {
String str = "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
Intent act = new Intent(context, MainActivity.class);
act.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
act.putExtra("message", str);
context.startActivity(act);
abortBroadcast();
}
}
}
}
}

application not going to intent part

this is my application code below which nto going to intent part i used debug i find is not going after line
xmlResponse2[0][i]=test[i];
wht i mistake?? can anyone help me is not launch any activity and code broke after
xmlResponse2[0][i]=test[i]; this line
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
String[][] xmlResponse2= null;
String[] test = str.split("\n");
xmlResponse2= new String[0][test.length];
for (int i = 0; i < test.length; i++) {
xmlResponse2[0][i]=test[i];
Intent l = new Intent(context,AgAppMenu.class);
l.putExtra("msg",xmlResponse2);
l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(l);
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
This statement:
xmlResponse2= new String[0][test.length];
creates an array with 0 elements (ie: with NO elements). Maybe you want to create an array with 1 element, like this:
xmlResponse2= new String[1][test.length];
xmlResponse2= new String[test.length];
for (int i = 0; i < test.length; i++) {
xmlResponse2[i]=test[i];
}
Intent l = new Intent(context,AgAppMenu.class);
l.putExtra("msg",xmlResponse2);
// l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(l);

String operation in array

how to resolve this error The operator += is undefined for the argument type(s) String[][], String
SmsMessage[] msgs = null;
String [][]str = 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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
The error pretty much says it all - you can't use += with an array in Java. Why not use a List to build up the String values and then convert it into an array when you are finished?
List<String> str = new ArrayList<String>();
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str.add("SMS from " + msgs[i].getOriginatingAddress());
str.add(" :");
...
String[] strArray = str.toArray(new String[str.size()]);

Categories

Resources