Runnable update lag - android

My app is a basic clock and i use Runnable for updating the time and the Drawable, but it seems to have some lag in every minute its like 2 or 3 times, its freeze for aboute 2 sec.
This is my runnable code:
private Runnable updateTimeTask = new Runnable() {
public void run() {
updateTime();
handler.postDelayed(this, 1000);
}
Dose someone know what method to use, or how to fix this issue, its not fine for the clock to have missing seconds and drawable lag.

The delay is probably the amount of time it takes to process updateTime(). Try starting the new timer before you call updateTime().
private Runnable updateTimeTask = new Runnable() {
public void run() {
handler.postDelayed(this, 1000);
updateTime();
}

Related

How to execute a function with a delay of a few seconds in Android programming

I have two functions. The first function takes 8 seconds and the second function takes 5 seconds. I want the second function to run with a delay of 3 seconds and the program not to stop and the first function not to stop.
How can I do this?
Please help me.
Use the Handler method :
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
yourfuction(); //This function will only work after 3 second
}
}, 3000);
Handler handler = new Handler();
Runnable myRunnable = new Runnable() {
public void run() {
// Things to be done
callYourFunction();
handler.postDelayed(this, 3000); //after every 3 sec call your function
}
};
handler.postDelayed(myRunnable, 3000);

Interrupting execution of handler/timer if user input occurs Android

I am trying to make feature that will display text on screen and after few sec to disappear. I have managed that with Handler and Timer classes.
The problem is that I need to somehow stop executing these timers if user makes input over keyboard before timer's time pass and rerun timer again to display different data.
I am facing with problem that the view has remaining visible after user input and disappears after 1-2 sec instead after 5 sec.
the codes that I have used:
//do something
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//do something again
}
}, 5000);
}
AND
//do something
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// this code will be executed after 5 seconds
//do something again
}
}, 5000);
Can you help me to solve this problem?
THanks
For timer you can use timer.cancel(); and for handler you can use handler.removeCallbacks(runnable);
So just declare a Runnable runnable as a global variable, then instantiate it as below
handler.postDelayed(runnable = new Runnable() {
#Override
public void run() {
//do something again
}
}, 5000);
this is the runnable you will pass when you removeCallbacks. Similarly you can use this handler.removeCallbacks(null); this will stop all the handlers that have been declared. I would suggest that you declare both the handler and the timer as global variables and only instatntiate them when you are calling the timer tasks.

How can i set any function to repeat after specific amount of time?

public void scheduleAtFixedRate (TimerTask task, long delay, long period). This looks promising but i have no idea how to use it. Any help would be appreciated.It was on android developer site.
Maybe this demo helps you:
import java.util.*;
public class TimerDemo {
public static void main(String[] args) {
TimerTask tasknew = new TimerScheduleFixedRateDelay();
Timer timer = new Timer();
timer.scheduleAtFixedRate(tasknew, 500, 1000);
}
public void run() {
System.out.println("working at fixed rate delay");
}
}
You need to have a method called "run" in your class, that will be repeately executed.
Source.
You can create a timer task and schedule it at a fixed rate like this:
Timer timer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
// This method is called in a fixed interval
}
};
timer.scheduleAtFixedRate(task, delay, period);
If you need to interact with the UI in the TimerTask you should do it like this:
TimerTask task = new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
// Interact with UI here
}
});
}
};
public void scheduleAtFixedRate(TimerTask task,
long delay,
long period)
Android doc here.
Parameters:
task - task to be scheduled.
delay - delay in milliseconds before task is to be executed.
period - time in milliseconds between successive task executions.
The task (TimerTask) is the code which will be executed forever, every period milliseconds. Delay is the time (in ms or Date if you want) which the Timer should wait until the start of the TimerTask.
You should remember Timer will run in a different thread from UI thread, so if you need to update the UI you should use runOnUiThread etc. (See Xaver Kapeller answer)
It could be an example
TimerTask tasknew = new TimerTask()
{
#Override
public void run()
{
/* Something here */
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(tasknew, 500, 1000);
I noticed here everyone just posted an example so it's just an extension with an explanation.

Creating a timer

I'm trying to create a timer, that after the timer ends, will call a function...
For example, I have the function Foo. I want to create a timer, that after 1.5 seconds will call it..
Something like :
Timer(Foo(), 2000);
I have found this code :
private Handler handler = new Handler(); // Creating new handler
handler.postDelayed(runnable, 1500); // Creating a timer for 1.5 seconds
and this function :
private Runnable runnable = new Runnable()
{
#Override
public void run()
{
Foo();
handler.postDelayed(this, 1500);
}
};
My problem is, that some times the timer works perfect, usually for the first 2~3 times, and after that, Instead of being a 1.5sec timer, it become something like 0.3sec timer (and the more handler.postDelayed(runnable, 1500); is being called, the less time the timer will last (like, wont wait 1.5sec to call Foo, but much less)
Why is that ?
I know that in C++ if I write Console Applications, I can use Sleep.. Maybe I can just do something like this :
Sleep(1500);
Foo();
Thanks!
Edit: I have answered my own question.
You could use the Timer class in Android, and set a repeating timer, with a initial delay.
Timer timer = new Timer();
timer.schedule(TimerTask task, long delay, long period)
A TimerTask is very much like a Runnable.
See: http://developer.android.com/reference/java/util/Timer.html
I've used 2 timers :
handler.postDelayed(runnable, 1500); // Creating a timer for 1.5 seconds
this created a 1.5sec timer, while inside the timer loop :
private Runnable runnable = new Runnable()
{
#Override
public void run()
{
Foo();
handler.postDelayed(this, 1500);
}
};
I called handler.postDelayed(this,1500); again, which made 2 timers -> causing the time bug.

How can I create timer tick in Android?

I have this method
public void GetSMS(){
//in this method I read SMS in my app inbox,
//If have new SMS create notification
}
for this I think create timer tick method and every 5 sec call GetSMS()
How can I create a correct method for that ?
Here is an example of Timer and Timer Task. Hope this helps.
final Handler handler = new Handler();
Timer timer = new Timer(false);
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
// Do whatever you want
}
});
}
};
timer.schedule(timerTask, 1000); // 1000 = 1 second.
Maybe with a timer and a timertask?
See javadocs:
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html
Yet receiving broadcasts is probably a more solid solution.
See: Android - SMS Broadcast receiver
Use Timer.scheduleAtFixedRate() as follow:
final Handler handler = new Handler();
Timer timer = new Timer(false);
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
GetSMS();
}
});
}
};
timer.scheduleAtFixedRate(timerTask, 5000, 5000); // every 5 seconds.
I saw it by accident.. This is not the right way to do it..
You don't need to check if there is a sms that received. Android provide broadcast receiver to get notified when sms is income.
Here you go, you have the link here.. Copy paste and it will work great
http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_Example/index.php?view=article_discription&aid=62&aaid=87
Hope that this make sense
Although the above timer methods are the correct way to use timers of the sort you are after, I quite like this little hack:
new CountDownTimer(Long.MAX_VALUE, 5000)
{
public void onTick(long millisUntilFinished)
{
// do something every 5 seconds...
}
public void onFinish()
{
// finish off when we're all dead !
}
}.start();
Long.MAX_VALUE has, according the Java docs, a (signed) value of 2^63-1, which is around 292471 millennia ! So starting up one of these countdown timers effectively lasts forever relatively speaking. Of course this depends on your interval time. If you want a timer every 1 second the timer would "only" last 58494 millenia, but we don't need to worry about that in the grander scheme of things.

Categories

Resources