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
public void updateAllTimers(View view) {
        _groupTimerWorker = new GroupTimerWorker(_displayLines);
        _groupTimerWorker.run();

    }

Open in new window

It crashes


If I use
public void updateAllTimers(View view) {
        _groupTimerWorker = new GroupTimerWorker(_displayLines);
      new Thread (_groupTimerWorker).start();
    }

Open in new window

it runs but does not update

If 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();
            }
        }
    }

Open in new window

It updates fine each time

I 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();
            }
        }
    }
}

Open in new window



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);
    }
}

Open in new window

LVL 1
jetbetAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jetbetAuthor Commented:
Update

public void updateAllTimers(View view) {
       _groupTimerWorker = new GroupTimerWorker(_displayLines);
       new Thread (_groupTimerWorker).run();
    }

Open in new window


and
public void run() {
        while (!_stopping) {
            try {

                Thread.sleep(1000);

                if (!_paused) {
                    updateAllTimers();
                }
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
            _stopping = true;
        }

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.
Chris Harte2015 Top Expert (Most Article Points)Commented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jetbetAuthor Commented:
Thanks for that.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Android

From novice to tech pro — start learning today.