Hello,

So I'm working on an app that should read sent sms in inbox for every
10 min.
I use a logic to compare the device's time and the time sms is sent.
And if the time sms is sent > the time of the device the toast will
pop up and show the sms
But I can't get the result I want, the toast is always giving the same
sms all the time.
So could anyone help me?

Here is my code:

package com.sent.sms;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class SentSMS extends Service{
        int saveDate = 0;
    int saveMonth = 0;
    int saveYear = 0;

    int saveHour = 0;
    int saveMinute = 0;
    int saveSecond = 0;

    int date = 0;
    int month = 0;
    int year = 0;

    int hours = 0;
    int minutes = 0;
    int seconds = 0;

    String[] projection = new String[]{"_id", "address", "body",
"date"};
    Uri uriSMSURI = Uri.parse("content://sms/sent");

    private Context ctx;

    private static Timer timer = new Timer();

        @Override
        public void onCreate() {
                super.onCreate();
                ctx = this;
                timer.scheduleAtFixedRate(new mainTask(), 0,60000);
        }

        @Override
        public IBinder onBind(Intent intent) {
                return null;
        }

        private class mainTask extends TimerTask{
        public void run(){
                doAllThing.sendEmptyMessage(0);
        }
    }

        private final Handler doAllThing = new Handler(){
        @Override
        public void handleMessage(Message msg){
                checkSMS();
                saveTime();
        }
    };

        private void saveTime(){
            date = Calendar.getInstance().get(Calendar.DATE);
            month = Calendar.getInstance().get(Calendar.MONTH)+1;
            year = Calendar.getInstance().get(Calendar.YEAR);
        hours = new Date().getHours();
        minutes = new Date().getMinutes();
        seconds = new Date().getSeconds();
        }

        public void checkSMS(){
              String sms = "";
              Cursor cursor = null;

              try {
                    cursor = getContentResolver().query(uriSMSURI
                            , projection
                            , null //selection
                            , null //selectionArgs
                            , null); //sortOrder

                    if (cursor != null && cursor.moveToFirst()) {
                        do {
                            String to =
cursor.getString(cursor.getColumnIndex("address"));
                            String body =
cursor.getString(cursor.getColumnIndex("body"));

                            String dateSMS =
cursor.getString(cursor.getColumnIndex("date"));
                            String monthSMS =
cursor.getString(cursor.getColumnIndex("date"));
                            String yearSMS =
cursor.getString(cursor.getColumnIndex("date"));

                            String hourSMS =
cursor.getString(cursor.getColumnIndex("date"));
                            String minuteSMS =
cursor.getString(cursor.getColumnIndex("date"));
                            String secondSMS =
cursor.getString(cursor.getColumnIndex("date"));

                            SimpleDateFormat formatDate = new
SimpleDateFormat("d");
                            SimpleDateFormat formatMonth = new
SimpleDateFormat("M");
                            SimpleDateFormat formatYear = new
SimpleDateFormat("yyyy");

                            SimpleDateFormat formatHour = new
SimpleDateFormat("H");
                            SimpleDateFormat formatMinute = new
SimpleDateFormat("m");
                            SimpleDateFormat formatSecond = new
SimpleDateFormat("s");

                            dateSMS = formatDate.format(new
Date(Long.parseLong(dateSMS)));
                            monthSMS = formatMonth.format(new
Date(Long.parseLong(monthSMS)));
                            yearSMS = formatYear.format(new
Date(Long.parseLong(yearSMS)));

                            hourSMS = formatHour.format(new
Date(Long.parseLong(hourSMS)));
                            minuteSMS = formatMinute.format(new
Date(Long.parseLong(minuteSMS)));
                            secondSMS = formatSecond.format(new
Date(Long.parseLong(secondSMS)));

                            saveDate = Integer.parseInt(dateSMS.trim());
                            saveMonth = Integer.parseInt(monthSMS.trim());
                            saveYear = Integer.parseInt(yearSMS.trim());

                            saveHour = Integer.parseInt(hourSMS.trim());
                            saveMinute = Integer.parseInt(minuteSMS.trim());
                            saveSecond = Integer.parseInt(secondSMS.trim());

                            if(date == 0 && month == 0 && year == 0 && hours ==
0 && minutes == 0 && seconds == 0){
                                sms += 
"Time:"+saveDate+"/"+saveMonth+"/"+saveYear
+"/"+" - "+saveHour+":"+saveMinute+":"+saveSecond+"To:"+to+"Body:"+body
+"\n";
                            }else{

                                if(saveMinute > minutes){
                                        sms += 
"Time:"+saveDate+"/"+saveMonth+"/"+saveYear
+"/"+" - "+saveHour+":"+saveMinute+":"+saveSecond+"To:"+to+"Body:"+body
+"\n";
                                    }else if(saveHour > hours){
                                        sms += 
"Time:"+saveDate+"/"+saveMonth+"/"+saveYear
+"/"+" - "+saveHour+":"+saveMinute+":"+saveSecond+"To:"+to+"Body:"+body
+"\n";
                                    }else if(saveDate > date){
                                        sms += 
"Time:"+saveDate+"/"+saveMonth+"/"+saveYear
+"/"+" - "+saveHour+":"+saveMinute+":"+saveSecond+"To:"+to+"Body:"+body
+"\n";
                                    }else if(saveMonth > month){
                                        sms += 
"Time:"+saveDate+"/"+saveMonth+"/"+saveYear
+"/"+" - "+saveHour+":"+saveMinute+":"+saveSecond+"To:"+to+"Body:"+body
+"\n";
                                    }else if(saveYear > year){
                                        sms += 
"Time:"+saveDate+"/"+saveMonth+"/"+saveYear
+"/"+" - "+saveHour+":"+saveMinute+":"+saveSecond+"To:"+to+"Body:"+body
+"\n";
                                    }
                            }
                        } while (cursor.moveToNext());
                    }
                } finally {
                    if (cursor != null) {
                        cursor.close();
                }
            }
                Toast.makeText(this, sms,Toast.LENGTH_LONG).show();
        }
}

Thank you very much.
Sorry for the long post :)

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to