0

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);


            }
Hayya ANAM
  • 575
  • 2
  • 14
  • 38

1 Answers1

1

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*/ }
    }
}
Jaffa
  • 12,442
  • 4
  • 49
  • 101
  • how do i set in my code i try it like str += msgs[i].getMessageBody().toString(); str += "\n"; str.split("\n"); str[0] contains "Hello" 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); – Hayya ANAM Sep 03 '12 at 07:44
  • You don't even declare str, what is it ? Simply work on `msgs[i].getMessageBody().toString().split("/n");` – Jaffa Sep 03 '12 at 07:46
  • Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String str = ""; – Hayya ANAM Sep 03 '12 at 07:47
  • @HayyaANAM "but its show error" -- What error ? I didn't tested my code, but I don't think I messed up too much :) – Jaffa Sep 03 '12 at 07:49
  • i edit my code now see again how i set this line in my code parts[0] contains "Hello" // parts[1] contains "World!" – Hayya ANAM Sep 03 '12 at 07:50
  • Try to take your time and read what you did! This ` str += msgs[i].getMessageBody().toString().split("/n");;` will obviously don't work. And what the use of getting each line if you always test on `str` ? – Jaffa Sep 03 '12 at 07:52
  • Str store incoming message body and sender number – Hayya ANAM Sep 03 '12 at 07:53
  • geoffory u say me above to do this like msgs[i].getMessageBody().toString().split("/n"); – Hayya ANAM Sep 03 '12 at 07:54
  • im cofuse its say do not convert String str t0 String [] str – Hayya ANAM Sep 03 '12 at 08:09
  • What are you talking to again? Never said you should convert `str` to `String[]`... What doesn't fit your needs in the code I posted? – Jaffa Sep 03 '12 at 08:11
  • read my code aboove just only tell me how i read sms in this way 00 LOG 00 AdeelAslam – Hayya ANAM Sep 03 '12 at 08:13
  • The part after "So your code may look like:" in my answer already told you how to proceed. – Jaffa Sep 03 '12 at 08:17