I want to make a method (service, alarm, etc.) that can be calculated after x downtime user with the app
Which closes the current activity
and will send the initial activity (login)
Thank you very much
http://androidbite.blogspot.in/2012/11/android-count-down-timer-example.html
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
Refer this link and some examples on countdown timer if you want to use this.
I answer
code is
private long startTime=15*60*1000; // 15 MINS IDLE TIME
private final long interval = 1 * 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countDownTimer = new MyCountDownTimer(startTime, interval);
}
#Override
public void onUserInteraction(){
super.onUserInteraction();
//Reset the timer on user interaction...
countDownTimer.cancel();
countDownTimer.start();
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
//DO WHATEVER YOU WANT HERE
// CIERRA LA APP MATANDO EL PROCESO Y VUELVE A ABRIRLO.
android.os.Process.killProcess(android.os.Process.myPid());
}
#Override
public void onTick(long millisUntilFinished) {
}
}
use
act.finishAffinity();
act.startActivity(new Intent(act, actMain.class));
Related
I need progress bar with interval from two timestamp values in millis.
For example, I have timestamp of 08:46:11 30.06.2019 and 10:46:11 30.06.2019. But current time is 09:46:11 30.06.2019, so now progressbar should be filled on 50% and going up to 100 until 10:46:11 30.06.2019.
So I have tried next code:
private void progressBar(){
prBar.setProgress(i);
mCountDownTimer=new CountDownTimer(ltimestampStop * 1000,ltimestampStart * 1000) {
#Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress"+ i+ millisUntilFinished);
i++;
int Start = ltimestampStart.intValue();
int Stop = ltimestampStop.intValue();
prBar.setProgress((int)i*100/(Stop/Start));
}
#Override
public void onFinish() {
//Do what you want
i++;
prBar.setProgress(100);
}
};
mCountDownTimer.start();
}
But problem is that int cannot handle my timestamp in millis and setProgress() didn't work with long type.
What I can try for achieving my task?
UPD: depending on my task I think countdowntimer is not what I need.
Initialize your progressbar and CountDownTimer like this -
MyCountDownTimer myCountDownTimer;
`ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressbar);`
progressBar.setProgress(100);
Make your countdowntimer like this -
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
Log.d("Time in millis", String.valueOf(millisUntilFinished));
int progress = (int) (millisUntilFinished/100);
progressBar.setProgress(progress);
}
#Override
public void onFinish() {
Log.d("Time in millis", "Timer finished");
progressBar.setProgress(0);
}
}
Call your CountDownTimer like this-
myCountDownTimer = new MyCountDownTimer(50000, 1000);
myCountDownTimer.start();
where should I edit this code, that i can stop the CountDown ?
How to delete the old one before I start the next new CountDown ?
public void MyCounter1(){
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
}
}.start();
}
here is code:
CountDownTimer timer= null;
public void MyCounter1(){
timer =new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
}
};
timer.start();
}
// To stop & start new timer check not null of timer instance first then cancel existing timer & start new one
if(timer != null){
timer.cancel();
MyCounter1();
}
also if you want to cancel first instance and start new one you can add above lines in onFinish method which will be get called when timer finish his time.
check this:
CountDownTimer timer= null;
public void MyCounter1(){
timer =new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
txt_timer.setText("Left time : " + millisUntilFinished / 1000);
}
public void onFinish() {
txt_timer.setText("done");
if(timer != null){
timer.cancel();
MyCounter1();
}
}
};
timer.start();
}
I did these 2 lines. I works at the time fine:
public void MyCounter1(){
CountDownTimer countDownTimer = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
if(my_condition=1){ //1- just make my_condition<>1 to stop the counter
txt_timer.setText("Left time : " + millisUntilFinished / 1000); }
}
public void onFinish() {
countDownTimer.cacel(); // 2- delete old one
txt_timer.setText("done");
}
}.start();
}
private int time = 10; //example 10 min.
CountDownTimer(60000 * time, 1000)
#Override
public void onFinish() {
txt_timer.setText("done");
cancel();
time = 0;
In my activity i want to check if countdowntimer is finished i'll stop the activity and pass to another activity?How can i do this?
I define countdowntimer like this
mcountdowntimer = new CountDownTimer(25000,1000) {
#Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
i++;
mProgressBar.setProgress(i);
}
#Override
public void onFinish() {
i++;
mProgressBar.setProgress(i);
}
};
mcountdowntimer.start();
mProgressBar.setProgressTintList(ColorStateList.valueOf(Color.rgb(64,91,164)));
}
Just simply add that logic in your onFinish() method:
#Override
public void onFinish() {
//here add the code for starting a new activity
}
i have countdown timer from 1 to 9999 if i click start button the count will start, but if click stop button i need to get current value from countdown and display that value in toast but the countdown could not stop if i click stop button please help me
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView ;
private final long startTime = 9999 * 1;
private final long interval = 1 *1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
countDownTimer = new MyCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime / 1));
}
public void onClick(View v) {
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("STOP");
} else {
/*countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESTART");*/
}
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
//text.setText("Time's up!");
countDownTimer.start();
}
#Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished / 1);
}
}
thank you
Here is my countdown timer:
QuestionCountdownTimer
public class QuestionCountdownTimer extends CountDownTimer {
private TextView remainingTimeDisplay;
private Context context;
public QuestionCountdownTimer(Context context,long millisInFuture, long countDownInterval,TextView remainingTimeDisplay) {
super(millisInFuture, countDownInterval);
this.context = context;
this.remainingTimeDisplay = remainingTimeDisplay;
}
#Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
remainingTimeDisplay.setText(hms);
}
#Override
public void onFinish() {
Toast.makeText(context,"COUNTDOWN FINISH :)",Toast.LENGTH_SHORT).show();
}
}
Note:
TextView remainingTimeDisplay
remainingTimeDisplay.setText(hms);
I use it to display the remaining time using a TextView
Here I call the timer:
//Start Quiz timer
QuestionCountdownTimer timer = new QuestionCountdownTimer(this,10000, 1000, remainingTimeDisplay);
timer.start();
-first parameter: this - I use it for context to show Toast message
-second parameter: 10000 - total time (10 sec)
-third parameter: 1000 - countdown interval (1 sec)
-last parameter: dispaly remaining time in real time
Tested and working
Create your CountDownTimer like this:
public class MyCountDownTimer extends CountDownTimer
{
private long timePassed = 0;
public MyCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
#Override
public void onFinish()
{
//text.setText("Time's up!");
countDownTimer.start();
}
#Override
public void onTick(long millisUntilFinished)
{
timePassed++;
text.setText("" + millisUntilFinished / 1);
}
public long getTimePassed()
{
return timePassed;
}
}
And on your onClick just do:
((MyCoundDownTimer) countDownTimer).getTimePassed();
to retrieve the time and set your textview text to it.
You should use handler
private Handler tickResponseHandler = new Handler() {
public void handleMessage(Message msg) {
int time = msg.what;
//make toast or do what you want
}
}
and pass it to MyCountDownTimer constructor
private Handler handler;
public MyCountDownTimer(long startTime, long interval, Handler handler) {
super(startTime, interval);
this.handler = handler;
}
And send message
#Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished / 1);
Message msg = new Message();
msg.what = millisUntilFinished/1;
handler.sendMessage(msg);
}
That's all you need to do :)
I have got an application where I need to show counter from 3 to 1 then quickly switch to another activity. Will TimerTask will be suitable for doing this? Can anybody show me an example of exactly how to do it?
CountDownTimer Worked. Code for showing timer for 3 seconds is.
new CountDownTimer(4000, 1000) {
public void onTick(long millisUntilFinished) {
Animation myFadeOutAnimation = AnimationUtils.loadAnimation(countdown.this, R.anim.fadeout);
counter.startAnimation(myFadeOutAnimation);
counter.setText(Long.toString(millisUntilFinished / 1000));
}
public void onFinish() {
counter.setText("done!");
}
}.start();
I would better use a CountDownTimer.
If you want for example your counter to count 3 seconds:
//new Counter that counts 3000 ms with a tick each 1000 ms
CountDownTimer myCountDown = new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
//update the UI with the new count
}
public void onFinish() {
//start the activity
}
};
//start the countDown
myCountDown.start();
Use CountDownTimer as shown below.
Step1: create CountDownTimer class
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
dialog.dismiss();
// Use Intent to Navigate from this activity to another
}
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
}
Step2: create an object for that class
MyCount counter = new MyCount(2000, 1000); // set your seconds
counter.start();