How to display a timer in user interface for android - java

I am implementing an application in which i need to display a timer in the layout.The app continues like once the timer countdowns to 0sec then it should proceed to another app automatically whose path is defined.

you can do like this and in onFinish load the other app
private class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(final long millisUntilFinished) {
long min = 0, sec = 0, totalSec = 0;
totalSec = (millisUntilFinished/1000);
min = totalSec/60;
sec = totalSec%60;
final long m = min;
final long s = sec;
runOnUiThread(new Runnable() {
public void run() {
System.out(" "+ m +"m and "+s+"s remaining.");
// or Display the way you want
}
});
}
#Override
public void onFinish() {
//load the task you want to do
}
}
can call it like this
MyCount counterr = new MyCount(sec *1000 , 1000);// sec = number of seconds
counterr.start();

I would have used CountDownTimer for this task. Use the onFinish method to call other activity.
new CountDownTimer(timetocomplete, ticks) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
// Intent to start a new activity
}
}.start();

Related

How will i add background service for countdown timer

long duration = TimeUnit.MINUTES.toMillis((long) 6.3);
new CountDownTimer(duration, 1000) {
#Override
public void onTick(long l) {
String sDuration = String.format(Locale.getDefault(),"%02d : %02d"
,TimeUnit.MILLISECONDS.toMinutes(l)
,TimeUnit.MILLISECONDS.toSeconds(l) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(l)));
orderCountdown.setText(sDuration);
}
#Override
public void onFinish() {
orderCountdown.setVisibility(View.GONE);
Toast.makeText(context,"asdasd",Toast.LENGTH_LONG).show();
}
}.start();
}
how can i add a background service for this code to work in background

How to countDownTimer.cancel(); in OnTick(); function

Hi i am trying to cancel a countdown timer in On-tick function, It is working fine in lollipop but not working in Kitkat and below. How cancel it from a OnTick() function.
private void startTimer(final int minuti) {
countDownTimer = new CountDownTimer(60 * minuti * 1000 + sec * 1000,
500) {
#Override
public void onTick(long leftTimeInMilliseconds) {
CountDownTimer.cancel();
}
#Override
public void onFinish() {
}
}.start();
}
Somehow if you call countdowntimer.cancel of a CountDownTimer object in its own onTick method it won't work!
The simplest way to solve your problem is defining another CountDownTimer, same as the main one, in order to check the condition to call countdowntimer.cancel. The point is that you are calling objects cancel method from outside of its own onTick method.
Check the example below:
CountDownTimer countdowntimer;
CountDownTimer assist;
long millisuntillfinish = 10000;
int interval = 100;
countdowntimer = new CountDownTimer(millisuntillfinish, interval) {
#Override
public void onTick(long millisUntilFinished) {
// if(true) countdowntimer.cancel(); (doesnt work)
}
#Override
public void onFinish() {
}
};
assist = new CountDownTimer(timertillfinish, timerinterval) {
#Override
public void onTick(long millisUntilFinished) {
if (true) {
countdowntimer.cancel();
}
}
#Override
public void onFinish() {
assist.cancel();
}
};
I know this question is more than 6 years old, but calling super.cancel(); within onTick() did the trick for me.
Leaving this here for the next unfortunate soul looking for an answer to this question.
ie.
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
if(someCondition)
super.cancel();
}
public void onFinish() {
//do something else
}
}.start();
new CountDownTimer(time, 1000) {
public void onTick(long millisUntilFinished) {
if (((millisUntilFinished/1000)%5)==0){ //every 5 sec
if(true)
{
this.cancel();
}
}
}
public void onFinish() {
//do somethhing
}
}.start();
I have been looking around and I have found this post where they call to the cancel() method in the onTick() using an alternative to the Android countDownTimer.
I had also this problem, and I created a global variable of a countDownTimer and I called to the cancel method in the activity when I needed, like myCountDownTimer.cancel().
private CountDownTimer mCountDownTimer;
private CountDownTimer createTimer(){
return new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//do what you want
}
public void onFinish() {
//do what you want
}
}.start();
}
In your activity call:
mCountDownTimer = this.createTimer();
And if you want to cancel it:
mCountDownTimer.cancel();

textview does not display seconds counting down Android

i have a textview that has a int number of seconds to count down from all the way to 0. i pass in a random number selected by a user to the timer and it counts down to 0. so i pass the variable S into timer
my textview:
mSeconds = (TextView)findViewById(R.id.secondsLabel);
my timer:
int s = //int taken from on spinner in another class
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
finish();
}
}, s * 1000);
new CountDownTimer(s, 1000) {
public void onTick(long millisUntilFinished) {
mSeconds.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
finish();
}
};
my textview does not display anything at all? how can i get it to show the seconds counting down?
You are not starting the CountDownTimer.
Do this
new CountDownTimer(s, 1000) {
public void onTick(long millisUntilFinished) {
mSeconds.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
finish();
}
}.start();
Start your count down timer
CountDownTimer ct = new CountDownTimer(s, 1000) {
public void onTick(long millisUntilFinished) {
mSeconds.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
finish();
}
};
ct.start();
You should manage it on UI thread

How can i Reset my Timer value meanwhile completion of timer?

I am trying to start a timer when activity created and be able to reset the timer back from zero if the same button is pressed but every time I press the button that initiates the set Interval it seems to be creating a new interval and not resetting the one that was already creating. Can someone help please? Here is my code
timer = (TextView) findViewById(R.id.timer_value);
Count = new CountDownTimer(time, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
timer.setText("OUT OF TIME!");
if (time < 10000) {
time = 10000;
}
AlertDialog.Builder builder = new AlertDialog.Builder(
TicTacToe.this);
builder.setMessage("You are Out of Time").setPositiveButton(
"Replay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// reset the game environment.
Count.onFinish();
// Count.cancel();
Count.start();
new_game(player_name_1);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}.start();
you can use timers
//in global
Timer myTimer
/**
* its kills runnable
*/
public void stopTimer(){
//handler.removeCallbacks(null); //it resets all timer which handler holds
myTimer.cancel();
}
public void setTimer(int time){//give it 5 for 5 secs
final Runnable Update = new Runnable() {
public void run() {
//do sth
}
};
myTimer = new Timer();
myTimerTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(Update);
}
}, 1000, time*1000);
}
If you want to restart your timer, remove Count.onFinish();
Update:
I replaced AlertDialog with a new thread and here is the code that works on my emulator
public class MainActivity extends Activity
{
int time = 10000;
CountDownTimer Count;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView timer = (TextView) findViewById(R.id.timer);
Count = new CountDownTimer(time, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
timer.setText("OUT OF TIME!");
if (time < 10000) {
time = 100000;
}
}
}.start();
}
public void buttonClicked(View view)
{
Log.i("Timer", "Resetting timer");
Count.cancel();
Count.start();
}
}

Android Countdown timer, continue timer on new activity

I've created a countdowntimer that works but it starts again on each activity. I'm trying to save the countdown value and use it in the second activity. Below is the countdown code on my main activity page.
//Countdown Start
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
final int j = (int) millisUntilFinished / 1000;
long timed;
timed = millisUntilFinished;
TextView textic = (TextView) findViewById(R.id.textView2);
textic.setText("" + (millisUntilFinished));
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView textic = (TextView) findViewById(R.id.textView2);
textic.setText(Integer.toString((j)));;
}
});
}
public void onFinish() {
}
}.start();
//Countdown Finish
I've tried storing the countdown value and then using passextra in my intent to pass "timed" but that didn't work. I've searched around and can't find any solution that works. Any help would be greatly appreciated.
Thanks
================
EDIT
================
public class MainActivity extends startscreen{
private CountDownTimer timer3;
public void onCreate(){
timer3 = new CountDownTimer(10000,100){
public void onTick(long millisUntilFinished) {
TextView displaytime = (TextView) findViewById(R.id.textView3);
displaytime.setText("" + (millisUntilFinished));}
public void onFinish() {
}
}.start();
};;
public void startTimer(){
}
public void cancelTimer(){
}
==============
EDIT 2
//Counter 1
Counter1 = new CountDownTimer(10000 , 1000) {
public void onTick(long millisUntilFinished) {
mCounter1TextField.setText("Seconds left: " + formatTime(millisUntilFinished));
}
public void onFinish() {
mCounter1TextField.setText("Finished!");
// Counter1.start();
}
};
Counter1.start();
public void starttimer() {
Counter1.start();
}
And then in my second activity i've got:
MainActivity Counter1 = (MainActivity) getApplicationContext();
Counter1.starttimer();
TextView mCounter1TextField=(TextView)findViewById(R.id.textView4);
mCounter1TextField.setText("Seconds left: " + (Counter1));
Extend the Application class and add your global timer there. Your class would look something like this
public class MyApplication extends Application{
private CountDownTimer timer;
public void onCreate(){
super.onCreate();
timer = new CountDownTimer(10000,100){
.....
};
}
public void startTimer(){
...
}
public void cancelTimer(){
...
}
}
Start this timer by making a call to getApplicationContext() from an activity
MyApplication application = (MyApplication) getApplicationContext();
application.startTimer();
Also, make sure you rename your application in your Android manifest.
<application ... android:name=".MyApplication" .../>
I would suggest either writing the CountDownTimer in its own class and extend it as a Service and then start the service when you want to start the timer, or make it a Singleton so all activities access the same timer.

Categories

Resources