I am running a thread in background contains timer and I display this timer in TextView
but when I change current fragment and I return the timer still running in logcat
but does not update in UI
My code:
final Runnable r = new Runnable() {
public void run() {
if (mBound) {
long elapsedTime = stopWatchService.getElapsedTime();
formattedTime = DateUtils.formatElapsedTime(elapsedTime);
Log.i(TAG, "formattedTime "+ formattedTime);
textView.setText(formattedTime);
}
}
};
Task task = new Task(r);
task.execute((Void) null);
My task:
public class Task extends AsyncTask<Void, Void, Void> {
private static final String TAG = "Task";
private boolean mPaused;
private Runnable mRunnable;
public Task(Runnable runnable, TextView textView) {
mRunnable = runnable;
play();
}
@Override
protected Void doInBackground(Void... params) {
while (!isCancelled()) {
if (!mPaused) {
mRunnable.run();
sleep();
}
}
return null;
}
private void sleep() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.w(TAG, e.getMessage());
}
}
public void play() {
mPaused = false;
}
public void pause() {
mPaused = true;
}
public void stop() {
pause();
cancel(true);
}
public boolean isPaused() {
return mPaused;
}
}
My service:
public class StopWatchService extends Service {
private String TAG = "walkingApp";
StopWatch stopWatch = new StopWatch();
public StopWatchService() {
}
public class LocalBinder extends Binder {
public StopWatchService getService() {
return StopWatchService.this;
}
}
private final IBinder mBinder = new LocalBinder();
public IBinder onBind(Intent intent) {
return mBinder;
}
/*
When the service is started, start stopwatch.
*/
public int onStartCommand(Intent intent, int flags, int startId) {
stopWatch.start();
return START_STICKY;
}
/* return how much time has passed in seconds */
public long getElapsedTime() {
return stopWatch.getElapsedTime();
}
@Override
public boolean stopService(Intent name) {
stopWatch.stop();
return super.stopService(name);
}
@Override
public void onDestroy() {
super.onDestroy();
// stopWatch.stop();
}
}