I have a textview, and I'm highlighting it dynamically (first 110 letters are highlighted first then after 1 second next 110 letters are highlighted and so on..). Below is my code for it.
I just created background thread as timer, but it is not stopping at all. How do I stop the timer after 3 iterations? Thanks in advance...
int x=0;,y=110//global values
Timer timer = new Timer();
//Create a task which the timer will execute. This should be an implementation of the TimerTask interface.
//I have created an inner class below which fits the bill.
MyTimer mt = new MyTimer();
//We schedule the timer task to run after 1000 ms and continue to run every 1000 ms.
timer.schedule(mt, 1000, 1000);
class MyTimer extends TimerTask {
public void run() {
//This runs in a background thread.
//We cannot call the UI from this thread, so we must call the main UI thread and pass a runnable
if(x==330)
Thread.currentThread().destroy();
runOnUiThread(new Runnable() {
public void run() {
Spannable WordtoSpan = new SpannableString(names[0]);
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), x, y, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
x=x+110;
y=y+110;
textView.setText(WordtoSpan);
}
});
}
}
did you try Handler instead of timer Task?
private static int TIME_OUT = 3000;
//--------------
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// do your task here
}
}, TIME_OUT);
There are some disadvantages of using Timer
It creates only single thread to execute the tasks and if a task takes too long to run, other tasks suffer. It does not handle exceptions thrown by tasks and thread just terminates, which affects other scheduled tasks and they are never run
Related
I have this AsyncTask, that sleeps 0.1 second each time that executes "doInBackground", before that I was using a CountDownTimer to control my time.
My problem is: I want to achieve a timer that can Pause without calling .cancel() and when starts creating another timer.
Is there a way to achieve this is android? I didn't found how to do it in a different way. Can you example it?
Where I've found examples canceling the timer:
How to stop the Timer in android?
Android timer? How-to?
http://www.androidhub4you.com/2013/04/countdown-demo-in-android-custom-timer.html
EDIT
Answering Kevin Krumwiede: This project is a kind of game, that I must hit blocks in a determinated time, so I want to achieve a way to stop the timer when the player use some kind of special power (hit a button specified).
EDIT2
Answering Kushal: I don't know if you have compiled this code, but I can't use the task1 variable. Here goes the code:
public void doIt(){
final ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
final Runnable task =
new Runnable() {
#Override
public void run() {
timerAsyncTask.execute();
if(true) {
exec.shutdown(); // shutdown this execution
//exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(task, 0, 100, TimeUnit.MILLISECONDS);
}
}
};
exec.scheduleAtFixedRate(task, 0, 100, TimeUnit.MILLISECONDS);
}
Here exec = Executors.newSingleThreadScheduledExecutor(); it shows me an error:
Error:(33, 21) error: cannot assign a value to final variable exec
What I think it's pretty okay, once the exec is a final variable.
And here exec.scheduleAtFixedRate(task, 0, 100, TimeUnit.MILLISECONDS); I got another error:
Error:(34, 46) error: variable task might not have been initialized
Can you explain, how can I use this piece of code? I'm quite new to android.
Thanks.
I suggest you avoid Timers in Android altogether. Android has a light weight and better solution called Handler
You can find a comparison between these two here. To sum up
Comparison Handler VS Timer
While rescheduling Handler is very easy, you can not reschedule Timer
In Handler you can attach to any Runnable but Timer schedule for only
one TimerTask
TimerTask is purely background task so you can not update
UserInterface, but that's not true for Handler's Runnables
Timer Causes Execptions
Timer tends to leak more memory compare to Handler see the graph of
object retains by timer and Handler. It will increase rapidly for
Timer if you are creating and scheduling new task.
As the post suggests, the Handler usage is pretty simple. Here is an example code
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
public class TestActivity extends AppCompatActivity {
private static int INITIAL_TIMER_DELAY = 100;
private static int TIMER_PERIOD = 100;
private android.os.Handler handler;
private Runnable runnable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler(Looper.getMainLooper());
runnable = new Runnable() {
#Override
public void run() {
// Do something and reschedule ourself
handler.postDelayed(this, TIMER_PERIOD);
}
};
handler.postDelayed(runnable, INITIAL_TIMER_DELAY);
}
#Override
protected void onDestroy() {
super.onDestroy();
cancelTimer();
}
private void cancelTimer() {
if (handler != null) {
handler.removeCallbacks(runnable);
}
}
private void rescheduleRunnable() {
handler.postDelayed(runnable, INITIAL_TIMER_DELAY)
}
}
You can achieve your requriment using ScheduledExecutorService class
Basic difference between ScheduledExecutorService and Timer class is :
Using Timer class, you cannot check how your execution is going. You can start and stop execution but cannot check execution based on condition
ScheduledExecutorService provides way to check how execution is running in between start and stop call. If any execution of the task encounters an exception, subsequent executions are suppressed
How we can achieve your requirement :
We need 0.1 second delay between doInBackground() execution
We shall be able to pause our execution when other execution starts
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
Runnable task1 = new Runnable() {
public void run() {
// start your AsyncTask here
new <your_task>().execute();
if (<check_your_condition_when_to_pause>) {
exec.shutdown(); // shutdown this execution
exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(task1, 0, 100, TimeUnit.MILLISECONDS);
// here i have passed task1 for example
// here we need to pass next task runnable which
// we want to run after 0.1 seconds
} else {
// continue normal
}
}
};
exec.scheduleAtFixedRate(task1, 0, 100, TimeUnit.MILLISECONDS);
Credit reference : Answer 1 and Answer 2
I hope this will help to solve some of your doubts
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.
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.
I would like to do an operation every 100ms for 1000ms.
I believe I would need to use the
handler
How do I do that?
Handler h = new Handler();
int count = 0;
int delay = 100;//milli seconds
long now = 0;
h.postDelayed(new Runnable(){
public void run(){
now = System.currentTimeMillis();
//do something
if(10>count++)
h.postAtTime(this, now + delay);
},
delay};
Please note that your operation MUST take less then 100ms to execute or it will not be able to run every 100ms, this will be the case for all methods.
Timer t = new Timer();
int count = 0;
t.scheduleAtFixedRate(new TimerTask() {
count++;
// Do stuff
if (count >= 10)
t.cancel();
}, 0, 100);
This schedules a timer to execute a TimerTask, with a 0 millisecond delay. It will execute the body of the TimerTask every 100 milliseconds. Using count to keep track of where you are in the task, after 10 iterations, you may cancel the timer.
As #Jug6ernaut mentioned, ensure your task won't take long to execute. Lengthy tasks (ones that take longer than 100 milliseconds, in your case) will cause lag/potentially undesirable results.
You can do this by using a Timer.
I don't have time to test this right now, but this should work
This is one way:
Your methods you want to call from here will probably need to be static
This class can be nested in another class
You could use % (modulus) so the timer can keep counting up and you can set things to happen at more intervals
create this timer:
private Timer mTimer = new Timer();
to start this timer:
mTimer.scheduleAtFixedRate(new MyTask(), 0, 100L);
the timer class:
/**
* Nested timer to call the task
*/
private class MyTask extends TimerTask {
#Override
public void run() {
try {
counter++;
//call your method that you want to do every 100ms
if (counter == 10) {
counter = 0;
//call method you wanted every 1000ms
}
Thread.sleep(100);
} catch (Throwable t) {
//handle this - maybe by starting it back up again
}
}
}
On button click I want to begin a timer of 5 minutes and then execute a method that will check for certain conditions and set off alerts if conditions are right. I've seen examples with timers and postDelay, but don't really understand why one would use one vs another. What is the best way to accomplish what I am trying to do? I don't want to lock up the UI during the 5 minutes. The user should be free to use the app as normal during the countdown.
EDIT: I am trying the postDelayed suggestion but visual studio is not liking something about my code. It looks exactly like examples I've found. My be a mono for android thing.
Handler h = new Handler();
Runnable r = new Runnable(){
public void run()
{
Dialog d = inst2.showBuilder(this, "test", "test");
d.Show();
}
};
h.postDelayed(r, 5000);
Specifically the code block inside of run throws all kinds of "} expected" and "a namespace cannot directly contain members such as fields or methods" exceptions.
Try using Timer Object :
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask()
{
#Override
public void run()
{
// Your code goes here
}
}, 1000); // 1sec
final Handler handler = new Handler();
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
#Override
public void run()
{
handler.post(new Runnable()
{
public void run()
{
// YOUR Code
}
});
}
}, 1000); // 1sec
You can start a simple Thread that will sleep in background for 5 minutes and then call a function. While the thread sleeps in background the UI will not freeze. When the thread finish executing what you want you can set off alerts by sending some intents as notifications and receive them in some Broadcast Receivers.
Hope this helps
Use Handler.postDelayed(Runnable block); method to execute delay, as android also not recommend to use timer.
Handler h = new Handler();
Action myAction = () =>
{
// your code that you want to delay here
};
h.PostDelayed(myAction, 1000);