How do you use a TimerTask to run a thread? - android

I'm struggling to find documentation for the TimerTask function on Android.
I need to run a thread at intervals using a TimerTask but have no idea how to go about this.
Any advice or examples would be greatly appreciated.

I have implemented something like this and it works fine:
private Timer mTimer1;
private TimerTask mTt1;
private Handler mTimerHandler = new Handler();
private void stopTimer(){
if(mTimer1 != null){
mTimer1.cancel();
mTimer1.purge();
}
}
private void startTimer(){
mTimer1 = new Timer();
mTt1 = new TimerTask() {
public void run() {
mTimerHandler.post(new Runnable() {
public void run(){
//TODO
}
});
}
};
mTimer1.schedule(mTt1, 1, 5000);
}

You use a Timer, and that automatically creates a new Thread for you when you schedule a TimerTask using any of the schedule-methods.
Example:
Timer t = new Timer();
t.schedule(myTimerTask, 1000L);
This creates a Timer running myTimerTask in a Thread belonging to that Timer once every second.

This is perfect example for timer task.
Timer timerObj = new Timer();
TimerTask timerTaskObj = new TimerTask() {
public void run() {
//perform your action here
}
};
timerObj.schedule(timerTaskObj, 0, 15000);

Related

How can get the current time of a video to set it in textview..?

i had tried this:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
videoview.getCurrentPosition;
}
},0,1000);
but it does not work(it crashes).
it says:
"cant create handler inside thread that has not called looper.prepare" =/
Please , post your answer if you have one , i found this way.
final Handler handler = new Handler();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
handler.post( new Runneable(){
public void run (){
//Your function =D
}
});
}
},0,1000);

Repeat "beep" sound each 10 seconds as a service Android

Rx with timer looks like the way to go. If you are not up for it Handler could work as well.
http://reactivex.io/documentation/operators/timer.html
You can try using :
TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();
public void playBeep(){
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
repeatBeep();
}
});
}};
t.schedule(scanTask, 10000, 10000);
}
public void repeatBeep(){
mp.start();
}
and call t.cancel() when you want to stop the beep

Android Repeat Task

Android Repetitive Task
here is my code but the sound is not repeated
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
mPlayer.start();
}
});
}};
timer.schedule(task, 0, 60000);
if you use media player, you can use
mPlayer.setLooping(true);
by the way ,your repeat timer task is fine.

How can i stop a TimerTask Handler?

I am trying to stop a Timer or TimerTask, but the method doesn't destroy the task...
First the code how i set up the timertask:
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
load_unread();
Log.d("TIMER", "Timer set off");
}
});
}};
t=new Timer();
t.schedule(scanTask, 300, 10000);
and now I'm trying to "kill" this Task at onDestroy:
#Override
protected void onDestroy() {
super.onDestroy();
scanTask.cancel();
t.cancel();
t.purge();
handler.removeCallbacksAndMessages(null);
System.out.println("Chat destroyed");
}
but this doesn't work? Can you please help me finding a solution?
Thanks!
EDIT: I finally found the answer. Don't know why mine didn't work...
Here the code for everyone who has the same Problem. I think this is a better and more efficient solution anyway:
private Handler handler = new Handler();
runnable.run();
private Runnable runnable = new Runnable()
{
public void run()
{
//
// Do the stuff
//
handler.postDelayed(this, 1000);
}
};
and to stop:
handler.removeCallbacks(runnable);
taken from here:
https://stackoverflow.com/a/11640073/1956197
After looking at your edit, the only thing I'd suggest is to use handler.post(runnable); instead of runnable.run(); This way you are always executing your runnable on a separate thread. Otherwise, your first execution will run on the main thread, then future executions run inside the handler on a separate thread.
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
//
// Do the stuff
//
handler.postDelayed(this, 1000);
}
};
handler.post(runnable);
Cancel the TimerTask before setting it to null.
scanTask.cancel();
this example start the timer unitl destroyed
private lateinit var timerTask: TimerTask
timerTask = object : TimerTask() {
override fun run() {
Log.d("KTZ", "$minutes:$seconds");
timeRecordingLiveData.postValue("$minutes:$seconds")
seconds += 1;
if (seconds == 60) {
Log.d("KTZ", "$minutes:$seconds");
timeRecordingLiveData.postValue("$minutes:$seconds")
seconds = 0;
minutes += 1;
}
}
}
Cancel the timertask in onDestroy()
timerTask.cancel()

how to change something in a new TimerTask action in android?

i have a problem with the timerTask in android i have a code like this:
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
countInt = countInt + 1;
textview1.setText(countInt);
}
}, 1000);
every time the timer task get startet my app crashed, i thing because i'm accessing the textview and it is in a other thread right?
how to solve this?
Yes, you are right, it crashes cause' you are accessing views from not an UI thread. To solve this, you can post a Runnable to UI thread using your activity
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
countInt = countInt + 1;
YourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
textview1.setText(countInt);
}
});
}
}, 1000);
try this..
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
countInt = countInt + 1;
yourActivity.this.runOnUiThread(new Runnable()
public void run(){
{textview1.setText(String.valueOf(countInt))});
}
}
}, 1000);
It crashes because you are messing with something ( textview1.setText(countInt);) that belongs UI thread which is not allowed...

Categories

Resources