Hi there
I have a problem in my food orders app I want to create countdouwn timer in my recyclew view depends on start order time and finish after 2 minutes and after finish cancel the order ...
The problem is
1 - how to every order has a different timer from start order to 2 minutes
2 - how order timer still working even app is close and if app not open automatically cancel the order ...
android main
I am trying everything to do that but iam filed...
Iam reading about how to run countdouwn in background and also reading about how to display countdouwn timer in recyclew
But cant found solutions to my cases...
Any help please...
Use a class for example, Dish. Now have a member of that class named as cancelTimer or whatever you want to call it. Using Handler, make the countdown for that particular instance. This way you can have different timers for each of your dishes.
Example:
Dish dish1 = new Dish();
dish1.setCancelTimer(10000);
Handler h = new Handler();
h.postDelayed(new Runnable(){
public void run(){
dish1.setCancelTimer(dish1.getCancelTimer()-1000);
h.postDelayed(this, 1000);
}
}, 0);
Repeat for all other dishes like:
Dish dish2 = new Dish();
dish2.setCancelTimer(50000);
Handler h2 = new Handler();
h2.postDelayed(new Runnable(){
public void run(){
dish1.setCancelTimer(dish1.getCancelTimer()-1000);
h.postDelayed(this, 1000);
}
}, 0);
Related
I need to create an alarm every time new data is inserted on the Database(the database has the values of date and time of every alarm that is going to be created), without any interaction with the user, the app has to do it automatically.
How do I know when new data is inserted? checking every x minutes using a timer?
How to create an alarm automatically when that happens?
Typically for something like this, you would want to use something more like Firebase that will live update your data.
But to do a timer, first extend TimerTask:
public class Progress extends TimerTask {
#Override
public void run(){
//this item runs on a separate thread
runOnUiThread(new Runnable() {
#Override
public void run() {
seconds += 0.1;
//this item runs on the main thread
}
});
}
}
And then to run this task:
Timer timer;
Progress progress;
timer.schedule(progress, 100, 100);
This will run the timer every .1 seconds.
I have a button on which I want to set the timer for 5 seconds for the first time and it should perform some task after completing 5 seconds. Also if user click button 2 times it should start timer for 10 seconds and after 10 seconds it should perform specific task. and if user click 3rd time it should stop all running timers. so I have do not know How to implement timer for one time
what I have search is this. But in this link it is continuously repeating after specific period of time, whereas I want to run once.
Now what I want
To start timer with first click (of 5 seconds)and if meanwhile user click 2nd time it should set timer with with new time period and if user click third time it cancels out all timers.
I do not want to use Thread timer using sleep method.
I want same behavior as there is in camera app in android 5.0 v.
So please tell me how to do this any code and source code would be appreciated.
In the link you provided you will find the answer if you try little harder.
For a repeating task:
new Timer().scheduleAtFixedRate(task, after, interval);
For a single run of a task:
new Timer().schedule(task, after);
So what you need to do is to maintain temporary variable to keep track of number of clicks and you can use second method like
For a repeating task:
new Timer().scheduleAtFixedRate(task, after, interval);
For a single run of a task:
new Timer().schedule(task, after * numberOfTimeBtnClked);
You have to pass the TimerTask method instead of task in that method.
**For updating your textview use below code and forget about whatever I have written above **
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//run in an interval of 1000ms
timer.schedule(timerTask, 0, 1000); //
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
timerSince++; //global integer variable with default value 0
if(timerSince == 5 * numberOfBtnClick){
//call your method
timer.cancel;
timerSince = 0;
}else{
//textView.setText(((5 * numberOfBtnClick)-timerSince)+" second left");
}
});
}
};
}
}
On event start button click call:
startTimer();
I am currently developing a stopwatch but I dont understand how can I add a countdown timer to it which has animations..
It should be triggered at the click of Start Button of the stopwatch and at the same time I have a mp3 sound clip of 3...2...1...Go which should be played with the countdown.. And after that the stopwatch should be triggered...
Sorry for placing so much demands but please give your answers in detail as in which file to put which command...
P.S :- I am using ADT Eclipse provided by Google Inc.
try this
long minute = 1000*60;//convert millescond to minute
long close_minutes = 5*minute;//set for 5 minutes
Handler handler = new Handler();
Runnable r=new Runnable() {
#Override
public void run() {
//do something count down is finished
}
};
handler.postDelayed(r, close_minutes);
I'm trying to create a count down timer, and it has to be like that:
The user will enter number of minutes in timerActivity and press "play"
The timerActivity will go to background and the user returned to previous activity
name commentsActivity.
every minute the user get a "Toast" saying how much time left.
I tried many possibilities including "chrono" and i will appreciate the help
Thanks in advanced.
You can start the user input activity with startWithResult and let the first activity handle the timer with the toast.
try it to using the runnable
Handler handler = new Handler ();
int i = the minute in second
handler.postDelayed(runnable, 1000L);
Runnable runnable = new Runnable() {
#Override
public void run() {
i = i - 1;
// make a Toast using i
handler.removeCallbacks(runnable);
handler.postDelayed(runnable, 1000L);
}
};
I want to run some code for 20 seconds precisely. It is similar to a loop but instead of having a variable I have time (in seconds).
I should have a time condition like this:
do
{ variable++ }
while (sec < 20)
How it is possible to do this in Android??
My application should run this 20 sec code after the user presses a button.
You can use the Handler class in Android on a runnable and then use the postDelayed() method. That way you will be able to update the UI during that 20 seconds on the progress of the thread. A good example of this is hear. Your code might look something like this ...
Handler handler = new Handler();
final Runnable r = new Runnable(){
public void run() {
//Do thing after 20 sec
}
};
handler.postDelayed(r, 20000);