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.
Related
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);
I have a timer and timer task and handler and runnable
my code be execute every 10 seconds until ServerResponse variable in not empty and then redirect to another activity.
but when my code redirect to another activity timer task is working !!!!!
how can to stop timer task when we are in another activity??
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
new sendDataToServer().execute();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(serverResponse.length() > 0)
{
Intent intent = new Intent(PayementActivity.this,UserFormActivity.class);
startActivity(intent);
finish();
}
}
}, 10000);
}
});
}
}, 0, 10000);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
new sendDataToServer().execute();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(serverResponse.length() > 0)
{
Intent intent = new Intent(PayementActivity.this,UserFormActivity.class);
if(timer!= null) {
timer.cancel();
timer.purge();
timer= null;
}
startActivity(intent);
finish();
}
}
}, 10000);
}
});
}
}, 0, 10000);
This might not answer your question directly, but just reactive extensions are a nice alternative to using TimerTasks. Check this and this out.
subscription = Observable.interval(10, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())// Runs on a io thread pool
.observeOn(AndroidSchedulers.mainThread()) // Observes on UI thread
.subscribe(timeCount ->{
// Your code here
});
subscription.unsubscribe();// Stops the stream.
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
I want to repeat the song perodic by 1minutes.
when the song finish playing wait 1 minute and restart
Please help
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();
mPlayer.setLooping(true);
}
});
}};
timer.schedule(task, 0, 60000);
You need to subscribe to the completion event in order to know when the song finishes.
Once the event is fired, than, start your delayed execution (no need for timer).
final Handler mHandler = new Handler();
mPlayer.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mHandler .postDelayed(new Runnable() {
#Override
public void run() {
mPlayer.start();
}
}, 60000);
}
});
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);