Hello! I was advised to use this code to make my method execute after a definite period of time, i modified it a bit and now I have:
private Handler mHandler = new Handler();
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
final long start = mStartTime;
// Get the difference in ms
long millis = SystemClock.uptimeMillis() - start;
// Format to hours/minutes/seconds
int mTimeInSec = (int) (millis / 1000);
// Do your thing
Location location = tracker.getLastKnownLocation(best);
RetrieveAvgPower(location);
// Update at the next second
mHandler.postAtTime(this, 1000);//start + ((mTimeInSec + 10) * 1000));
}
};
And I try to start and stop it with:
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
mStartTime = System.currentTimeMillis();
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 1000);
break;
case R.id.stop:
mHandler.removeCallbacks(mUpdateTimeTask);
break;}}
However, there is an issue. First of all, my method sets text and writes a line to log file, but if I use the code above text is not being set, however all info is being written to log file automatically. Another thing is i can't stop runnable - after it starts executing program seems to be not responding and crashes if I try to press stop button. What am I doing wrong and how it can be solved?