2

I want to read the messages of particular number in a particular time. For example i want to read the before one month messages of "+91934345432".( For example today aug 6th so i need the messages in between july 6th to aug 6th. )

my code:

Calendar c = Calendar.getInstance();

    long date2 = c.getTimeInMillis();

    c.add(Calendar.DAY_OF_YEAR, -30);

    long date1 = c.getTimeInMillis();
    Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
    Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, "datetime(date/1000, 'unixepoch') between " + date1 + " and " + date2 + " ", null, null);
    startManagingCursor(cursor1);

    String[] columns = new String[] { "address", "person", "date", "body","type" };
    if (cursor1.getCount() > 0) {
        String count = Integer.toString(cursor1.getCount());
        while (cursor1.moveToNext()){
            String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
            String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
            body = cursor1.getString(cursor1.getColumnIndex(columns[3]));
            String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
            String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
         //   System.out.println("Int Date : " + new Date(((long)Integer.parseInt(date))*1000L));
            if(address.contains("AD-AIRMTA")&& body.contains("Recharge")){


                Log.v("Tag_b0dy",""+body);



            }

        }
    }

result: Tag_b0dy:null

Thanks in advance

kartheeki j
  • 2,206
  • 5
  • 27
  • 51

2 Answers2

0

You can add selection to your query as:-

Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, "datetime(date/1000, 'unixepoch') between date1 and date2", null, null);

date1 is 6th July in milliseconds and date2 is 6th august in milliseconds

For getting date1 and date2 you can user java.util.Calendar as

Calendar c = Calendar.getInstance();

long date2 = c.getTimeInMillis();

c.add(Calendar.DAY_OF_YEAR, -30);

long date1 = c.getTimeInMillis();

So final query will look like this

Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, "datetime(date/1000, 'unixepoch') between " + date1 + " and " + date2 + ", null, null);
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
0
 Calendar c = Calendar.getInstance();

        date2 = c.getTimeInMillis();

        c.add(Calendar.DAY_OF_YEAR, -30);

  long  MonthAgo = c.getTimeInMillis();

 Uri  qryinBox = Uri.parse("content://sms/inbox");
        Cursor qryinBoxRes = getContentResolver().query(qryinBox, new String[]{"_id", "thread_id", "address", "person", "date", "body", "type"}, null, null, null);
        startManagingCursor(qryinBoxRes);

        String[]   columns = new String[] { "address", "person", "date", "body","type" };

        if (qryinBoxRes.getCount() > 0) {
            String count = Integer.toString(qryinBoxRes.getCount());
            while (qryinBoxRes.moveToNext()){
                String address = qryinBoxRes.getString(qryinBoxRes.getColumnIndex(columns[0]));
                long SMSdate = qryinBoxRes.getLong(qryinBoxRes.getColumnIndex(columns[2]));
                body = qryinBoxRes.getString(qryinBoxRes.getColumnIndex(columns[3]));
                String name = qryinBoxRes.getString(qryinBoxRes.getColumnIndex(columns[1]));
                String type = qryinBoxRes.getString(qryinBoxRes.getColumnIndex(columns[4]));
                if(address.contains("+9189245454489")&&SMSdate > MonthAgo){

// here u will get that one month msseges of particular number
}
kartheeki j
  • 2,206
  • 5
  • 27
  • 51