java - 如何停止/启动或重新启动计时器线程

标签 java android multithreading

我正在尝试使用线程来倒计时回答问题的秒数。 这是更新屏幕上 TextView 的剩余秒数 我想在回答问题后重新启动计时器。 如何停止/启动或重新启动计时器线程。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_thread_example"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ebookfrenzy.threadexample.ThreadExampleActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/myTextView"
        app:layout_constraintBottom_toBottomOf="@+id/activity_thread_example"
        app:layout_constraintRight_toRightOf="@+id/activity_thread_example"
        app:layout_constraintLeft_toLeftOf="@+id/activity_thread_example"
        app:layout_constraintTop_toTopOf="@+id/activity_thread_example" />

    <Button
        android:text="@string/press_me"
        android:layout_width="wrap_content"
        android:layout_height="51dp"
        android:id="@+id/button"
        app:layout_constraintRight_toRightOf="@+id/myTextView"
        android:layout_marginTop="32dp"
        app:layout_constraintTop_toBottomOf="@+id/myTextView"
        app:layout_constraintLeft_toLeftOf="@+id/myTextView"
        android:onClick="buttonClick" />
</android.support.constraint.ConstraintLayout>


package com.enceladussoftware.threadexample;

import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.util.Objects;

public class ThreadExampleActivity extends AppCompatActivity {

    private static String TAG = "Thread";

    Handler mhandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            Bundle bundle = msg.getData();
            String string = bundle.getString("myKey");
            TextView myTextView = (TextView) findViewById(R.id.myTextView);
            myTextView.setText(string);

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread_example);

        Log.i(TAG, "oncreate()");

    }

    public void buttonClick(View v) {

        int threadNumber = 1;

        final Thread myThread = new Thread("myThread" + threadNumber) {
            public void run() {

               Looper.prepare();

                Log.i(TAG, "Start run()");

                int i = 10;
                while (i>=0) {
                    Log.i(TAG, ( "Remaining: " + i + " seconds. "));
                    Message msg = mhandler.obtainMessage();
                    Bundle bundle = new Bundle();
                    String dateString = String.valueOf(i);
                    bundle.putString("myKey", dateString);
                    msg.setData(bundle);
                    mhandler.sendMessage(msg);
                    try {
                        i--;
                        Thread.sleep(1000L);    // 1000L = 1000ms = 1 second
                    } catch (InterruptedException e) {
                        //I don't think you need to do anything for your particular problem
                    }
                }

                Log.i(TAG, "End runabble()");
                Looper.loop();

                }

                };

                  myThread.start();
     }

  }

最佳答案

CountDownTimer counter = new CountDownTimer(30000, 1000){
    public void onTick(long millisUntilDone){
       Log.d("counter_label", "Counter text should be changed");
       tv.setText("You have " + millisUntilDone + "ms");                    
    }

    public void onFinish() {
        tv.setText("DONE!");
    }
};
counter.start(); 

counter.cancel(); // cancel
counter.start();  // then restart

关于java - 如何停止/启动或重新启动计时器线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39725787/

相关文章:

python - 无响应的请求 - 了解瓶颈(Flask + Oracle + Gunicorn)

Ubuntu 中的 java.lang.UnsatisfiedLinkError

android - 如何在 Eclipse 中重命名小部件 ID?

android - 转换为 Dalvik 格式失败

android - 如果我想要一个可以用于其他类(class)的计时器,我应该输入什么代码?

c++ - ofstream线程安全吗?

multithreading - 原子设置位函数: when it is useful for?

java - 我如何计算每个省份有多少男性和女性,以及这些省份是按国家/地区的字母顺序排列的?

java - 如何重置类内的值?

java - 使用 Java 8,当且仅当它具有空值时,如何为 List<Object[]> 中的 obj[4] 设置字符串常量?