Search code examples
androidmultithreadingdateutcrunnable

How to run my thread even during device Switch-Off in android


I have developed an application that runs every day once. In that I want to update the date by one in a File (so it needs to be done every day).
I make it possible with help of Handler like this:

final Runnable mUpdateResults = new Runnable() {
        public void run() {

            try{

                FileInputStream fia = openFileInput("ActualDate");
                StringBuffer actualContent = new StringBuffer("");
                byte[] buffer = new byte[1024];
                try {
                    while ((fia.read(buffer)) != -1) {
                        actualContent.append(new String(buffer));
                    }
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                actualDate = actualContent.toString().trim();

                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                Calendar c = Calendar.getInstance();
                try {
                    c.setTime(sdf.parse(actualDate));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                c.add(Calendar.DATE, 1);  
                actualDate = sdf.format(c.getTime()); 
                FileOutputStream foa = openFileOutput("ActualDate", Context.MODE_PRIVATE);
                foa.write(actualDate.getBytes());
                foa.close();

            }catch (Exception e) {
                // TODO: handle exception
                startupFunction();
            }

        }
    };

    long delay = 24*60*60*1000; // first run after a day

    long period = 24*60*60*1000; // repeat every day.

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {

    public void run() {

        mHandler.post(mUpdateResults);
    }

    }, delay, period);

The actual idea behind this is that I want a device independent date for my application which user cannot change. I store the first occurrence of date in ActualDate file from UTC date.

PROBLEM: The code works fine except when device is switched off and so thread stops for that time. How can I overcome this problem.

All kinds of suggestions are greatly appreciated. Thanks in advance.


Solution

  • Try to follow the logic below. Its just a thought, which may be helpful.

    Your application runs today - So you put an entry in your file, say myTodayDate. This is your application specific date.

    Along with this, also put the system current date, which will be stored as lastUpdatedDate.

    Say the mobile stays switched off for next 2 days. Later, when your application runs, you can do this.

    Take the lastUpdatedDate from the file and subtract it from the current system date. The difference you'll get will be as noOfDays=2.

    Add this to your myTodayDate which is there in your file.

    Now, store this new date, along with current system as told before. This may help you overcome your date sync problem.

    Just a thought actually! Let me know, if it helped.