Countdown timer stops running in background - android

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.

Related

Reuse a timer after cancelling

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();

How to just have one OnClick Countdown Timer running?

I am having trouble thinking of a way to keep only one CountdownTimer going for an onClick listener. Basically I have an application that 6 buttons and each time you hit one of the buttons it starts a countdown on the respective buttons text. However, I only want the most recent countdown started by an OnClick for each button running and updating the text on the button.
Here's the code I have so far... It works, but the countdowns start "fighting" with each other over which is altering the text of the button. Is there a way so that each time the OnClick is registered that any countdowns created by earlier button clicks are destroyed / ignored?
oBlue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Button was clicked so start our count down
CountDownTimer blueTime = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
String s = String.valueOf(millisUntilFinished / 1000);
oBlue.setText(s);
}
public void onFinish() {
oBlue.setText(String.valueOf(300));
}
}.start();
Replace your counter variable with an internal class, so that you do not need to create counter variable each time. Just create a time counter variable and call the start of it if you want to restart the counter each button.
Es:
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
//enter your code

How to stop media recorder when it come two seconds in android

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();

Legal use of countdown timer?

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.

Trying to set up a countdown timer in Android through multiple Activites

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?

Categories

Resources