I am using a countdown timer to perform a repeating task. I want to be sure what I'm doing is valid since I'm not sure if the countdown timer object gets destroyed when it times out. Same question applies if I call the cancel method. Here is my code:
public class MyCount extends CountDownTimer
{
public MyCount(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish()
{
new myAsyncTask().execute();
this.start();
}
#Override
public void onTick(long millisUntilFinished)
{
}
}
What you are doing is fine. The timer object will be freed only when the object is no longer reachable. That is, you can call timer.start() repeatedly on the same object as you are doing. timer.cancel() also does not free the object. You can call timer.cancel() and then call timer.start() to reset the timer, all on the same object.
Related
I am developing an application which has to use timer for some purpose. What I need to do is, to set a timer when a condition satisfies and to cancel it correspondingly. Then again I may start the same timer which I cancelled. Please provide a solution.
For a single timer android sets a single thread. When you cancel that timer its execution thread terminates gracefully, and no more tasks may be scheduled on it. So you have to create a new instance of the timer object.
you can do as shown below.
if(condition == true)
{
timer = new Timer();
timer.schedule(new TimerTask(), delay, span);
}
else{
timer.cancel();
}
Create CountDownTimer class like:
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
// do something of finish timer
}
#Override
public void onTick(long millisUntilFinished) {
// do something on tick of timer
}
}
and should make an object of MyTimer like below:
MyCount timerCount = new MyCount(<TIME_UNTIL_FINISH_MILLISECONDS>, <TIME_OF_EACH_TICK>);
and can start timer everywhere you want with:
timerCount.start();
and cancel it with:
timerCount.cancel();
I have implemented a countdown timer in an application of mine. It runs in the background fine and dandy, but when i use advanced task killer, it stops the timer and the only way to restart it is to open the application again. Is there anyway to have the timer persist, even if I use something like advanced task killer?
Code:
TextView tv;
final MyCounter timer = new MyCounter(10000,1000);
tv = (TextView)findViewById(R.id.healthtext);
tv.setText("10");
timer.start();
}
public class MyCounter extends CountDownTimer{
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
Toast.makeText(getApplicationContext(), "death", Toast.LENGTH_LONG).show();
}
#Override
public void onTick(long millisUntilFinished) {
tv.setText((millisUntilFinished/1000)+"");
as far as I know - nope, since task killers destroy your app's process causing any running threads to exit
Not while the timer is part of your application. You can of course make a timer that is not part of the application.
How do I stop the media recorder when it comes two minutes in android? I used stMaxduration and info listener, there is no guarantee in the call in the api.
Can anybody tell me any other way to achieve this? Provide code please
Thanks
The best way is to implement a countdown timer where you need to execute something after definite times. Below is the code. Here 120000 milliseconds represents two minutes and 1000 milliseconds represents 1 second "MyCount(120000, 1000)"
MyCount counter = new MyCount(120000, 1000);
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
//do your stuff here
}
#Override
public void onTick(long millisUntilFinished) {
}
}
Run using the threads when you stop the thread the media player will gets stop.
snippet:
Runnable r = new Runnable()
{
public void run()
{
mp1.start();
}
};
new Thread(r).start();
I'm trying to create a app for airports, that the top line will always display a countdown timer to when the plane leaves. This is the code I had.
This class is declared in each activity class. /countdowntimer is an abstract class, so extend it and fill in methods:
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
tv.setText(”done!”);
}
#Override
public void onTick(long millisUntilFinished) {
tv.setText(”Left: ” + millisUntilFinished/1000);
}
}
But I wanted it to run on all the activities. I was thinking about having the notification manger to start a thread every 10 seconds, but I cannot access the UI memory on its thread.
Does anybody have a good solution to how to do this?
Here is the link where i have discussed about a countdown timer of format(mm:ss) in Java:
Java console code for StopWatch/Timer?
Now i want to display (and update) it in a textview in android. Any clue?
To count down
You'll use a TextField and update its content using CountDownTimer, or check Rahul's answer in this same question.
To count up
In your Activity
import android.widget.Chronometer;
...
private Chronometer crono;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_crono);
this.crono = (Chronometer) findViewById(R.id.calling_crono);
startCrono();
}
public void startCrono() {
crono.setBase(SystemClock.elapsedRealtime());
crono.start();
}
To stop it
crono.stop();
In the XML Layout screen_crono
<Chronometer
android:id="#+id/calling_crono"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textStyle="bold"
android:textSize="14sp"/>
To set it inside a TextView, I don't think that's possible. Place it to its right or to its left, depending on what you want.
If this is not what you wanted, I hope it helps someone else.
You can also use the CountDownTimer class available in android.
Just declare a constructor and start the timer with
timer test=new timer(30000,1000);
onTick will get fired once in every 1000ms in the above case. You can update your TextView from here
class timer extends CountDownTimer
{
public timer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
#Override
public void onFinish()
{
}
#Override
public void onTick(long millisUntilFinished)
{
// TODO Auto-generated method stub
// Update your textview on on tick
}
}
Use the method of a timed UI update in this article it really works great
http://developer.android.com/resources/articles/timed-ui-updates.html
Also, you might use the built in android time class to display your text, it will make your formatting much easier imo
http://developer.android.com/reference/android/text/format/Time.html
try this code.....
tv = new TextView(this);
this.setContentView(tv);
//10000 is the starting number (in milliseconds)
//1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(10000,1000);
counter.start();
}
//countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer
{
public MyCount(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
public void onFinish()
{
tv.setText("Time Up!");
}
public void onTick(long millisUntilFinished)
{
tv.setText("Time Left : " + millisUntilFinished/1000);
}