BadHouses BadHouses - 7 months ago 19
Android Question

File Modification check on Android

I have written a loop that checks the file modification date on two files.
This is successful as far as the Toast text is concerned.
However I cannot seem to work out the syntax to actually compare the dates, comparing Date1 > Date2 is actually rejected by android studio.

Lots of advice on how to get the most recent modification date is on Stack Overflow, just not the answer as to how to craft the necessary if statement.

Any help would be most appreciated. (yes I have read documentation! Usage examples poor on this subject)

Handler mHandler = new Handler();
protected void onCreate(Bundle savedInstanceState) {
// Email sync loop (maybe change for watcher method)
new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(10000); Runnable() {

public void run() {
Toast.makeText(MainActivity.this, "Loop", Toast.LENGTH_SHORT).show();
File file = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/Database1.db")));
if (file.exists()) {
Date lastModified = new Date(file.lastModified());
String modified = lastModified.toString();
Toast.makeText(MainActivity.this, modified, Toast.LENGTH_LONG).show();
File file2 = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/Database2.db")));
if (file2.exists()) {
Date lastModified2 = new Date(file2.lastModified());
String modified2 = lastModified2.toString();
Toast.makeText(MainActivity.this, modified2, Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO: handle exception


You don't need the separate thread, the loop and the sleep() call, you can simply schedule the check on the Handler instance using postDelayed() method. It will be executed on the main thread which is perfectly fine for you and won't block the app. As a final step in your Runnable instance you can schedule it again, so it will run in a loop.

If you want to stop it then call removeCallbacksAndMessages(null) on the handler.

I am not sure what do you want to achieve with this, but maybe instead of doing a polling loop you can use FileObserver class for monitoring file changes, here is an example how to use it.