2016-09-27 226 views
0

我正在嘗試使用線程來倒計時以回答問題。 這是在屏幕上更新留給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(); 
    } 

    } 

回答

0

如果線程已終止,則無法重新啓動它。您可以執行的操作是創建一個新的Thread實例。

refer to this answer

0

定義你的線程對象buttonClick外面類變量

要停止你的線程,你可以把它稱爲

if(mThread.isAlive()) 
    mThread.interrupt(); 

而作爲一個新的實例時,您將需要爲計數再次啓動通過致電

mThread.start(); 
+0

一個類變量的例子,以及這個應該放在哪裏將是非常有用的(mThread.isAlive()) mThread.interrupt(); –

+0

作爲類變量意味着方法之外。如果(mThread.isAlive())mThread.interrupt();這應該放置在獨立的方法中,並在您想要停止它時調用該方法。 – Jai

1
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