jetbet
asked on
Android threading
I have an application that consists of a group of stop watches.
I have everything working except for the background thread updating each count
If I use
If I use
If I do the update manually
I want to be able to start and pause the updates. Does anyone know the correct syntax I should use?
Other code
I have everything working except for the background thread updating each count
If I use
public void updateAllTimers(View view) {
_groupTimerWorker = new GroupTimerWorker(_displayLines);
_groupTimerWorker.run();
}
It crashesIf I use
public void updateAllTimers(View view) {
_groupTimerWorker = new GroupTimerWorker(_displayLines);
new Thread (_groupTimerWorker).start();
}
it runs but does not updateIf I do the update manually
public void updateAllTimers(View view) {
for (int i = 0; i < _displayLines.size(); i++) {
TimerLine tl = _displayLines.get(i);
if (tl._isRunning) {
tl.SetCurrentDuration();
}
}
}
It updates fine each timeI want to be able to start and pause the updates. Does anyone know the correct syntax I should use?
Other code
package com.example.redkatipo.myfirstapp;
import java.util.List;
/**
* Created by Brian on 6/04/2015.
*/
class GroupTimerWorker implements Runnable {
Boolean _stopping = false;
Boolean _stopped = false;
Boolean _paused = false;
List<TimerLine> _lines;
public GroupTimerWorker(List<TimerLine> lines) {
_lines = lines;
}
public void Stop() {
_stopping = true;
}
public void SetPaused(Boolean pause) {
_paused = pause;
}
@Override
public void run() {
while (!_stopping) {
try {
Thread.sleep(1000);
if (!_paused) {
updateAllTimers();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void updateAllTimers() {
for (int i = 0; i < _lines.size(); i++) {
TimerLine tl = _lines.get(i);
if (tl._isRunning) {
tl.SetCurrentDuration();
}
}
}
}
package com.example.redkatipo.myfirstapp;
import android.content.Context;
import android.os.SystemClock;
import android.content.res.Resources;
import android.text.method.BaseKeyListener;
import android.widget.TextView;
import android.widget.Button;
/**
* Created by Brian on 27/03/2015.
*/
public class TimerLine {
public Context _context;
public Button _stopStart;
TextView _person;
public TextView _elapsed;
boolean _isRunning;
int _startTime = 0;
int _currentTime = 0;
int _previousTime = 0;
public TimerLine(Context context, Button _control, TextView _id, TextView _output) {
_context = context;
_stopStart = _control;
_person = _id;
_elapsed = _output;
_isRunning = false;
_stopStart.setTag(this);
_elapsed.setText("idle");
}
private int CurrentSeconds()
{
return (int)(SystemClock.uptimeMillis()/1000);
}
public void stopStartButtonClick()
{
if (_isRunning == false) {
_isRunning = true;
// _stopStart.setBackground(_context.getResources().getDrawable(R.drawable.red_button));
_stopStart.setText("Stop");
_elapsed.setBackground(_context.getResources().getDrawable(R.drawable.green_button));
_previousTime = _previousTime + _currentTime - _startTime;
_elapsed.setText(formatTime(_previousTime));
_startTime = CurrentSeconds();
} else {
_isRunning = false;
/// _stopStart.setBackground(_context.getResources().getDrawable(R.drawable.green_button));
_stopStart.setText("Start");
_currentTime = CurrentSeconds();
int difference = (_currentTime - _startTime);
int totalTime = difference + _previousTime;
_elapsed.setText(formatTime(totalTime));
_elapsed.setBackground(_context.getResources().getDrawable(R.drawable.red_button));
}
}
public void SetCurrentDuration()
{
int now = CurrentSeconds();
int difference = now - _startTime;
int totalTime = difference + _previousTime;
_elapsed.setText(formatTime(totalTime));
}
private String formatTime(int totalTime)
{
int minutes = totalTime/60;
int seconds = totalTime % 60;
return "" + minutes + ":" + String.format("%02d", seconds);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for that.
ASKER
Open in new window
and
Open in new window
works to update the timers. NOTE that I exit the loop by setting _stopping to true inside the loop.
Without the exit the gui locks up (accepts no more input)
It looks as though the thread is not run in the background and does not show the update until the function (run()) completes.