0

I have created a text file with simple date concept. Everyday the textfile will be stored on my system with that day's date. I am sending these text files to the server automatically using timer concept.

Now my question is I would like to send the previous day's text file to the server. For ex: if today is monday, and here as per my timer schedule at 8, I need to send sunday's(previous day) text file to server. I am stuck here and I don't know how to achieve this. Here is my codings

File mydir = new File("file path");
mydir.mkdirs();
final String filename =   new SimpleDateFormat("dd-MM-yyyy").format(new Date());
File outputFile = new File(mydir, filename);
FileOutputStream fos = new FileOutputStream(outputFile,true);
marsei
  • 7,691
  • 3
  • 32
  • 41
AndroidOptimist
  • 1,419
  • 3
  • 23
  • 38

2 Answers2

1

You need to define a service and use a handler which should initially set by the time difference from start time to 8'O clock. then the timer intervel should be 24 hours, Within the execution you can send the file...

Eg: you are installing the app at 5pm, you need to initially send the file at 8pm so you calculate the interval ((8-5) * 60 * 60 *60 * 1000) since handler use milliseconds. After that you can set it as 24 * 60 * 60 *60 * 1000

UPDATE

Use handler like this

private int mInterval = 5000; // 5 seconds by default, can be changed later
private Handler mHandler;

@Override
protected void onCreate(Bundle bundle) {

    mHandler = new Handler();
}

Runnable mStatusChecker = new Runnable() {
@Override 
public void run() {
  updateStatus(); //this function can change value of mInterval.
  mHandler.postDelayed(mStatusChecker, mInterval);
}
};

void startRepeatingTask() {
   mStatusChecker.run(); 
}

void stopRepeatingTask() {
   mHandler.removeCallbacks(mStatusChecker);
}

Here you need to change the interval as i explained earlier

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

What you need to do is build the previous day's file name and see if it exists, then send it to the server.

Follow these steps.

  1. Create an new date object
  2. Subtract a day from this - you will get the previous day's object
  3. Create the file name for your text file based on this object
  4. Create a file object with this path name
  5. Check if the file exists

If the file exists, send the file to the server else do nothing.

Edit: Assuming that you are able to create the file name for the previous day correctly,

String filePath = FILES_DIRECTORY + yesterdaysDate + FILE_EXTENSION;
File yesterdaysFile = new File(filePath);
if( yesterdaysFile.exists() ){
    // Upload this file to the server
}
midhunhk
  • 5,560
  • 7
  • 52
  • 83
  • the files with the previous date is there in my system. Now my question is how to sent these files automatically to the server and how to schedule this – AndroidOptimist Nov 28 '13 at 07:18
  • I thought you had already figured this out with the Timer and schedule etc as in your question. – midhunhk Nov 28 '13 at 07:20
  • using timer concept i will sent the file automatically to the server. But my question is i would like to sent the previous days file to server. Now i can sent only today's file to the server – AndroidOptimist Nov 28 '13 at 07:23
  • That is what I have mentioned in my answer, while creating the file path, use the date object and subtract a day. – midhunhk Nov 28 '13 at 07:30
  • I did as Date d = new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24)); My question is how to upload this file from the list of files – AndroidOptimist Nov 28 '13 at 07:42