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?
Related
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.
I want to write a countdown in android which starts counting from 3 to 0. Like at first 3 in appearing and then disappearing and 2 is appearing and so on. I searched a lot but I couldn't find any good sample. Can you help me that what should I do?
use CountDownTimer
For example:
import android.os.CountDownTimer;
MyCount timerCount;
public class TestCountdown extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timerCount = new MyCount(3 * 1000, 1000);
timerCount.start();
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
//some script here
}
#Override
public void onTick(long millisUntilFinished) {
//some script here
}
}
}
The good guys in Android thought about you.
You have a class for that - CountDownTimer.
I am not going to write you the code for this but this should not be difficult.Just use a thread to display the value 3(using say TextView) first then sleep for say (100ms assuming you want it to change after 1 sec) then decrease it and repeat.
An example would be
for i=0 to 3
print the number
thread.sleep(100)
am creating one application for student. I need to show digital clock with remaining time.
here i add digital clock
but it show system date
and i need to show Remaining time means suppose if exam time is 10 minute then it must show
time in reverse manner. Or i need to use another clock.
Any help is Appreciated.
Define this code in onCreate.
// 5000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(5000, 1000);
counter.start();
Constructor for CountDownTimer is as follow.
// 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!”); //TextView object should be defined in onCreate
}
#Override
public void onTick(long millisUntilFinished) {
tv.setText(”Left: ” + millisUntilFinished/1000);// This will be called every Second.
}
}
Take a look at the CountDownTimer-class.
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 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.