1

I have implemented a code which gets me the first sms from "Sent SMS" in string format.

I want to get all the sms from the sent box one by one starting from the first.

I would like to save them in SQLite database.

The below code shows the first sent sms in Toast but how do i get all the sent sms?

public class MainActivity extends Activity {

    String address, name, date, msg, type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button btn =(Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Uri mSmsinboxQueryUri = Uri.parse("content://sms/sent");
                Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
                            new String[] { "_id", "thread_id", "address", "person", "date",
                                            "body", "type" }, null, null, null);
                startManagingCursor(cursor1);
                String[] columns = new String[] { "address", "person", "date", "body","type" };
                if (cursor1.getCount() > 0) {
                    String count = Integer.toString(cursor1.getCount());
                    //Log.e("Count",count);
                    System.out.println("Count:" + count);
                    while (cursor1.moveToNext()){
                        address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
                        name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
                        date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
                        msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
                        type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
                    }
                }

                Toast.makeText(getApplicationContext(), address + "\n" + name + "\n" + date + "\n" + msg + "\n" + type, Toast.LENGTH_LONG).show();
            }
        });

    }


}
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Somnath Pal
  • 190
  • 1
  • 15

2 Answers2

0

use

cursor.moveToFirst(); 

before line

 while  (cursor1.moveToNext())
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
Ashish Rawat
  • 5,541
  • 1
  • 20
  • 17
  • Still showing only first sms from "Sent box" on button click. I want to get to the next one. Any other method by which I can get the sent sms data on string one by one so that I can add them in database? – Somnath Pal Oct 04 '15 at 13:04
  • Yes , you can use following, however your approach is also correct.. so it should also work .. http://stackoverflow.com/questions/19856338/using-new-telephony-content-provider-to-read-sms – Ashish Rawat Oct 07 '15 at 05:56
0

instead of while use

if(cursor1.moveToFirst())
{
   for (int i = 0; i <cursor1.getCount(); i++) 
   {
      address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
      name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
      date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
      msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
      type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
      cursor1.moveToNext();
   }
}

and also close cursor at end

fatima
  • 3
  • 6