0

我可以看到日期和时间的线程正在运行。但是秒表没有线程。可能是什么问题呢?

公共类 MainActivity 扩展 Activity {

private Button Start, Reset, Stop;
private EditText stop_watch, lblDate, lblTime;

public MainActivity() {

}

private final UpdateClockThread ucThread = new UpdateClockThread();
private final StopwatchThread swThread = new StopwatchThread();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Start = (Button)findViewById(R.id.button1);
    Stop = (Button)findViewById(R.id.button3);
    Reset = (Button)findViewById(R.id.button2);
    stop_watch = (EditText)findViewById(R.id.editText3);
    lblTime = (EditText)findViewById(R.id.editText2);
    lblDate = (EditText)findViewById(R.id.editText1);

    init();

    Start.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            startactionPerformed();
        }
    });

    Stop.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            stopactionPerformed();
        }
    });

    Reset.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            resetactionPerformed();
        }
    });

}

public void init() {
swThread.setDaemon(true);
ucThread.setDaemon(true);
swThread.start(); // this doesent seem to wrk..
ucThread.start();
}

/** Listens to the Start/Stop/Resume button. */

void startactionPerformed() {
    swThread.go();
}

void stopactionPerformed() {
    swThread.noGo();
}

void resetactionPerformed() {
    swThread.reset();
}

/** A thread that updates the current date & time. */
private class UpdateClockThread extends Thread {
/** The actual work of the thread. */
    public void run() {
        while (true) {
            MainActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    Calendar now = Calendar.getInstance();
                    String month = Integer.toString(now.get(Calendar.MONTH)+1);
                    String date = Integer.toString(now.get(Calendar.DAY_OF_MONTH));
                    String year = Integer.toString(now.get(Calendar.YEAR));
                    String hour = Integer.toString(now.get(Calendar.HOUR));
                    if (hour.equals("0")) hour = "12";
                    String minute = Integer.toString(now.get(Calendar.MINUTE));
                    if (minute.length() == 1) minute = "0" + minute;
                    String second = Integer.toString(now.get(Calendar.SECOND));
                    if (second.length() == 1) second = "0" + second;
                    String ampm = now.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM";
                    lblDate.setText(month + "/" + date + "/" + year);
                    lblTime.setText(hour + ":" + minute + ":" + second + " " + ampm);
                }
            });

            try {
                sleep(500);
            } catch (InterruptedException e) {} 
        }
    }
}

/** A thread that keeps track of the stop watch & updates
 * the display accordingly.
*/
class StopwatchThread extends Thread {
    /** Whether or not stop watch is running. */
    private boolean going = false;
    /** Stores elapsed milliseconds of previous runs. */
    private long prevElapsed = 0;
    /** Stores beginning time of this run. */
    private Date startDate = new Date();

    /** Returns elapsed time in milliseconds.
     *@return The elapsed time
     */
    private long elapsedTime() {
        return prevElapsed + (going ? new Date().getTime() - startDate.getTime() : 0);
    }
    /** Changes the number of elapsed milliseconds into a string.
     *@param time Number of elapsed milliseconds
     *@return The elapsed time as a string.
     */
    private String msToString(long time) {
        String ms, sec, min;
        if (time % 10 >= 5) //round to nearest hundredth
            time += 5;
        ms = Long.toString(time % 1000);
        while (ms.length() < 3)
            ms = "0" + ms;
        ms = ms.substring(0, ms.length() - 1);
    time /= 1000;
    sec = Long.toString(time % 60);
    if (sec.length() == 1) sec = "0" + sec;
    time /= 60;
    min = Long.toString(time);
    stop_watch.setText(min);
    return min + ":" + sec + "." + ms;
}

    /** Called when the stop watch is to go.
    */
    public void go() {
        startDate = new Date();
        going = true;
    }
    /** Called when the stop watch is to stop.
     */
    public void noGo() {
        prevElapsed = elapsedTime();
        going = false;
    }
    /** Resets the stop watch.
     */
    public void reset() {
        going = false;
        prevElapsed = 0;
    }
    /** Adds a lap to the list.
     */

    /** Main code of the thread.
     */

    public void run() {

        MainActivity.this.runOnUiThread(new Runnable() {
            public void run() {
                stop_watch.setText(msToString(elapsedTime()));          
            }
        });
        yield();
    }
} 

}

我可以看到日期和时间的线程正在运行。但是秒表没有线程。可能是什么问题呢?

我如何启动秒表线程..请帮助..

我的程序中有两个守护线程..但日志显示只有一个处于活动状态..即使我终止了活动线程。秒表仍然不工作。我尝试了很多。但我无法弄清楚问题出在哪里..

DalvikVM[本地主机:8633]

线程 [<1> main](正在运行)

线程 [<8> 活页夹线程 #2](正在运行)

线程 [<7> 活页夹线程 #1](正在运行)

守护线程 [<10> Thread-10](运行中)

4

1 回答 1

0

问题在于您的 StopwatchThread 的run()方法。

它在 UI 线程上运行单个任务(调用stop_watch.setText()),然后停止。您没有任何类型的循环,即使您定义了一个看起来很有希望的“进行”变量。

添加一个类似于 UpdateClockThread 中的 while 循环,我想您会发现它按预期工作。

于 2012-10-31T20:38:31.660 回答