Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: simple time counter

Tags:

android

timer

I have a simple program with one TextView and two Buttons: Button1 and Button2.

Clicking on Button 1 will start a counter, increasing by 1 every 1 second and show result on TextView; clinking on Button 2 will stop it. Here is a part of my code for Button1. But it does not work.

    Timer T=new Timer();     T.scheduleAtFixedRate(new TimerTask() {                      @Override             public void run() {                 myTextView.setText("count="+count);                 count++;                             }         }, 1000, 1000); 

I know that there are some similar questions about this using Thread but it seems like they do not mention about stopping the counter.

Any suggestion is really appreciated.

Added:

Hello, I just shortened my code just to this from a much bigger program, but it is still crashed:

package com.example.hello; import java.util.Timer; import java.util.TimerTask; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity {     TextView myTextView;     int count=0;     Timer T;     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         myTextView=(TextView)findViewById(R.id.t);         T=new Timer();         T.scheduleAtFixedRate(new TimerTask() {                      @Override             public void run() {                 myTextView.setText("count="+count);                 count++;                             }         }, 1000, 1000);            }     @Override     public boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.activity_main, menu);         return true;     }      } 

The log file (I do not want to post this because it is too long but someone requested it):

07-28 17:35:07.012: W/dalvikvm(11331): threadid=7: thread exiting with uncaught exception (group=0x4001d800) 07-28 17:35:07.016: E/AndroidRuntime(11331): FATAL EXCEPTION: Timer-0 07-28 17:35:07.016: E/AndroidRuntime(11331): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 07-28 17:35:07.016: E/AndroidRuntime(11331):    at android.view.ViewRoot.checkThread(ViewRoot.java:2802) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at android.view.ViewRoot.requestLayout(ViewRoot.java:594) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at android.view.View.requestLayout(View.java:8125) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at android.view.View.requestLayout(View.java:8125) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at android.view.View.requestLayout(View.java:8125) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at android.view.View.requestLayout(View.java:8125) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:254) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at android.view.View.requestLayout(View.java:8125) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at android.widget.TextView.checkForRelayout(TextView.java:5378) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at android.widget.TextView.setText(TextView.java:2688) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at android.widget.TextView.setText(TextView.java:2556) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at android.widget.TextView.setText(TextView.java:2531) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at com.example.hello.MainActivity$1.run(MainActivity.java:21) 07-28 17:35:07.016: E/AndroidRuntime(11331):    at java.util.Timer$TimerImpl.run(Timer.java:289) 
like image 321
DavidNg Avatar asked Jul 30 '12 22:07

DavidNg


People also ask

How do I put a countdown clock on my Android?

onTick(long millisUntilFinished ) - In this method we have to pass countdown mill seconds after done countdown it will stop Ticking. onFinish() - After finish ticking, if you want to call any methods or callbacks we can do in onFinish(). start() - It is used to call countdown timer.

What is Android CountDownTimer?

CountDownTimer app is about setting a time that moves in reverse order as it shows the time left in the upcoming event. A CountDownTimer is an accurate timer that can be used for a website or blog to display the count down to any special event, such as a birthday or anniversary.

Is there a free timer app?

Clockify is the most popular clock timer app for tracking the amount of time you spend on projects, tasks, and various activities. Start tracking time — It's Free!


2 Answers

You can introduce flag. Something like isPaused. Trigger this flag whenever 'Pause' button pressed. Check flag value in your timer task. Success.

Timer T=new Timer(); T.scheduleAtFixedRate(new TimerTask() {                  @Override         public void run() {             runOnUiThread(new Runnable()             {                 @Override                 public void run()                 {                     myTextView.setText("count="+count);                     count++;                                 }             });         }     }, 1000, 1000);    onClick(View v)  {       //this is 'Pause' button click listener       T.cancel();  } 
like image 195
Pavel Dudka Avatar answered Oct 16 '22 17:10

Pavel Dudka


Chekout this example, that uses the Chronometer class: http://android-er.blogspot.com/2010/06/android-chronometer.html

Using the Chronometer class will save you managing the thread yourself.

like image 33
hovanessyan Avatar answered Oct 16 '22 16:10

hovanessyan