App Stops refreshing after a couple of hours? - java

I build a printer app that i need to run 24/7 to check if there is new order, I wrote a code that refreshes the activity every 60 seconds to check if there's a new order. The problem is that it runs fine for an hour or so but after that the activity stops refreshing automatically until i click the refresh button. Then again it works for an hour or so and again stops refreshing automatically. Can somebody please help me with this?
I've use the following code to refresh the activity.
private static final int delay = 60;
Timer timer;
OrderAdapter oadaptor;
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
getLiveOrders(bID,"ALL");
oadaptor.notifyDataSetChanged();
}`enter code here`
});
}
}, 0, delay * 1000);

You shouldn't be doing that with a Timer but a service instead. That way it will continue running even if the app is in the background.

Related

Vaadin refresh grid after adding row

My grid is not refreshing automatically after adding a row. I've tried several solutions from other questions but they didn't work. E.g. grid.clearSortOrder(); and grid.markAsDirty();. My goal is to add rows after time periods. Therefore I'm using a Timer and the rows are added but the grid does not refresh until I click in the table.
Easy code example:
Grid grid = new Grid();
grid.addColumn("Name");
grid.addColumn("Age");
Timer timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
grid.addRow("Exmaple","99");
}
}, 1000, 1000);
You will Need Server push for this. See Vaadin-Doc-Serverpush.
You want to change the UI from another Thread (Timer.schedule() will execute in another thread).
Vaadin callbacks are triggered by user interactions like mouse press on a button, selection of an option and so on. When you need the user interface to reflect a change that was not caused by the mouse/keyboard, you need to enable server push:
#Push
public class App extends UI {
#Override
public void init(VaadinRequest request) {
timer.schedule(new TimerTask() {
public void run() {
access(() -> grid.addRow("Example", "99"));
}
}, 1000, 1000);
}
}
Note the usage of UI.access(Runnable) to lock the UI when it is accessed from a non-request thread.

Java timer working weird/not working

So I'm making a game and I need a timer to regend/deregen stamina from the player.
Here's my current code:
Timer staminaTimer = new Timer(); //Java.util timer
public void update() {
staminaTimer.schedule(new TimerTask(){
public void run() {
//regen/Deregen stamina
}
}, 0L, 10L * 1000L);
}
So this timer is in an method that is getting called every game tick. I don't know why but if I put a print in the run() its starts normally then becomes really fasts and crashes everything. I would really like to know another way of making a repeating timer or the fix of this... Thanks for help in advance.

LibGDX Timer doesn't start after game resumed on android

I'm creating 2 timers in my code. One is the logic timer that updates the logic every 0.017 seconds:
logicTimer = new Timer();
logicTimer.scheduleTask(new Timer.Task() {
#Override
public void run() {
updateLogic();
}
}, 0f, timePerProcessing);
And the other one is for generating obstacles every 3 seconds:
meteoroidTimer = new Timer();
meteoroidTimer.scheduleTask(new Timer.Task() {
#Override
public void run() {
generateMeteoroids();
}
},1f,3f);
When I pause my game and resume afterwards, my logic timer still works but my obstacle timer doesn't. I thought it was because I use a Random object in my method that I call in the timer, but I tried with a simple:
System.out.println("It is showing");
and it still doesn't resume.
My code for pause and resume:
#Override
public void pause(){
meteoroidTimer.stop();
logicTimer.stop();
}
#Override
public void resume(){
meteoroidTimer.start();
logicTimer.start();
}
You dont need to use the timer.stop and star on resume because when you leave the app libgdx stops rendering/updating anyway so your timers wont be updated anyway.

Android timer one thread only

I'm having problem with Android timer's scheduleAtFixedRate option.
Timer timer = new Timer();
TimerTask myTimerTask = new TimerTask() {
public void run() {
...
}
};
timer.scheduleAtFixedRate(myTimerTask, 0, 5000);
This snippet is doing bad things for me. It's executed in the service so every time method is called timer creates a new thread and executes the same code while the old thread is still running; that creates performance problems. I need to run the code in the run() method every 5 seconds but I want the old task to be canceled. Is there any way to handle this problem ?
You can use the timer.cancel() to stop the timer.
For example, I had an end button to finish the timer early:
finishEarlyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
timer.cancel();
}
});

Countdown clock in GWT

I want to create a countdown clock in GWT but I cannot find the right function that waits for one second. I tried with Thread.Sleep() but I think it is for another purpose.
Can you help me? This is my code.
int count=45;
RootPanel.get("countdownLabelContainer").add(countdown);
for(int i=count; i>=0; i--)
{
countdown.setText(Integer.toString(i));
// Place here the wait-for-one-second function
}
Give Timer a try (See Here).
Changing the example code real quick to something close to what you want, you'll want to buff this up for your purposes though:
public class TimerExample implements EntryPoint, ClickListener {
int count = 45;
public void onModuleLoad() {
Button b = new Button("Click to start Clock Updating");
b.addClickListener(this);
RootPanel.get().add(b);
}
public void onClick(Widget sender) {
// Create a new timer that updates the countdown every second.
Timer t = new Timer() {
public void run() {
countdown.setText(Integer.toString(count));
count--;
}
};
// Schedule the timer to run once every second, 1000 ms.
t.schedule(1000);
}
}
This sounds like something in the general area of what your looking for. Note that you can use timer.cancel() to stop the timer. You'll want to tie this in with your count (when 45 hits 0).
The following snippet showing the use of the timer works too. It shows how to schedule the timer properly and how to cancel it.
// Create a new timer that updates the countdown every second.
Timer t = new Timer() {
int count = 60; //60 seconds
public void run() {
countdown.setText("Time remaining: " + Integer.toString(count) + "s.");
count--;
if(count==0) {
countdown.setText("Time is up!");
this.cancel(); //cancel the timer -- important!
}
}
};
// Schedule the timer to run once every second, 1000 ms.
t.scheduleRepeating(1000); //scheduleRepeating(), not just schedule().

Categories