Android : Different behavior when debugging

on Thursday, September 25, 2014


I wrote an application that logs data coming from my smartphone's sensors. I have a thread that writes this data to a file every hour. When connected to my laptop with a USB cable and using adb logcat, the app works fine. Every hour a new file is created.


But when disconnecting the smartphone from my laptop it doesn't do that. The app ran the whole night and only one file was created...


My code uses Thread.sleep() is this an issue?


I am testing this an a Nexus 5 with android 4.4.4



@Override
public void run()
{
String filename = "";
String directory = "";
Date now = new Date();

while (!stopped)
{
Log.i(TAG, "Start the one hour sleep");
sleepForOneHour(now);
now = new Date();

directory = directoryDateFormat.format(now);
filename = filenameDateFormat.format(now);

Log.i(TAG, "Write data to SD-card");
setFileWriteDirectory(directory);
writeDataToSdCard(filename);
}
super.run();
}

private void sleepForOneHour(Date filesWritten)
{
Date now = new Date();
long nowMillies = now.getTime();
long filesWrittenMillies = filesWritten.getTime();

long passedMillies = (nowMillies - filesWrittenMillies);

long milliesPerHour = (60 * 60 * 1000);

long waitMillies = (milliesPerHour - passedMillies);

try
{
sleep(waitMillies);
} catch (InterruptedException e)
{
Log.d(TAG, "Interrupted sleep in filehandler thread");
}
}

0 comments:

Post a Comment