0

This is the code i am using to read the inbox message when view button is pressed in context menu but i am unable to read messages i get only the body of the first message.

 public class Smsread extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView view = new TextView(this);
  Uri uri = Uri.parse("content://sms/inbox");
  Cursor c= getContentResolver().query(uri, null, null ,null,null);
  startManagingCursor(c);
  String sms = "";
  if(c.moveToFirst()){
      for(int i=0;i<c.getCount();i++){
   sms= c.getString(c.getColumnIndexOrThrow("body")).toString();
  // sms=c.getString(c.getColumnIndexOrThrow("address")).toString();

      }
      c.close();
      c.moveToNext();

      /*
  sms ="From :" + c.getString(2) + " : " + c.getString(11)+"\n";         
  */
  }

  view.setText(sms);
  setContentView(view);

}
}              
dippu
  • 1
  • 1
  • 3

3 Answers3

2

Try this:

if(c.moveToFirst()){

      for(int i=0;i<c.getCount();i++){

         sms= c.getString(c.getColumnIndexOrThrow("body")).toString();
         c.moveToNext();
      }
}

You will have to move to next record for each iteration of for loop.then only,you will get all messages there.Here,in for loop,after you get String sms,you can use that sms to show up somewhere.Each time,it will grab next sms into it!

Hope you got the point!

Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • @parag: Thanks parag the logic you said is correct but i tried applying the same in my program but still when i press view button for each message i get the same body of first message. – dippu Feb 26 '12 at 05:30
  • @Hiral : i tried bro but its not working the same problem is still continueing.Help me plz.. – dippu Feb 27 '12 at 17:36
  • @dippu: but what is the problem you are facing with my lines of code?And i hope,you included required permissions to your manifest file. – Hiral Vadodaria Feb 28 '12 at 04:09
  • @Hiral: Actually I have used bundle to move my message content from one activity to another.I have a readsms.java file which reads the content of sms when clicked.the problem i am facing is in the list view i get proper num and corresponding content.But when i click on that particular list item to read the content i get the content of some other message.There is a problem in cursor.I am not able to figure it out. – dippu Mar 01 '12 at 04:45
  • @dippu: please post the complete code where you set your listview and override its onItemClickListener. Also post your custom adapter class. – Hiral Vadodaria Mar 01 '12 at 04:48
0

You must change

 sms= c.getString(c.getColumnIndexOrThrow("body")).toString();

to

 sms+= c.getString(c.getColumnIndexOrThrow("body")).toString();
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
Kaiser Helawe
  • 43
  • 1
  • 5
0

Try this:

String[] sms=new String[c.getCount()];

if(c.moveToFirst()){

      do{
         sms= c.getString(c.getColumnIndexOrThrow("body")).toString();

       } while(c.moveToNext());

}
chimbu
  • 786
  • 5
  • 16
  • 36