Android - Controlling a task with Timer and TimerTask?
You might consider:
- Examining the boolean result from calling
cancel()
on your task, as it should indicate if your request succeeds or fails - Try
purge()
orcancel()
on theTimer
instead of theTimerTask
If you do not necessarily need Timer
and TimerTask
, you can always use postDelayed()
(available on Handler and on any View
). This will schedule a Runnable
to be executed on the UI thread after a delay. To have it recur, simply have it schedule itself again after doing your periodic bit of work. You can then monitor a boolean flag to indicate when this process should end. For example:
private Runnable onEverySecond=new Runnable() {
public void run() {
// do real work here
if (!isPaused) {
someLikelyWidget.postDelayed(onEverySecond, 1000);
}
}
};
Timer and TimerTask in Android
You need to cancel() timer not the timer task.
Control View Pager Slides with Timer Task
I think Using Handler
is better then TimerTask
in this case if ViewPager
can slide manually too.
First Create a Handler
and Runnable
Globally.
private Handler handler=new Handler();
private Runnable runnable=new Runnable() {
@Override
public void run() {
if(pagerSlider.getCurrentItem()==data.size()-1){
pagerSlider.setCurrentItem(0,false);
}else{
pagerSlider.setCurrentItem(pagerSlider.getCurrentItem()+1);
}
}
};
Post the runnable inside onPageChange
.
pagerSlider.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
handler.removeCallbacks(runnable);
handler.postDelayed(runnable,2000);
}
});
You need to post for first time Rest the listener will do. Change the delay as per your need :-
handler.postDelayed(runnable,2000);
I just realize that you might be asking about scrolling velocity . Well for this you need to use Customize Scroller. Go to This thread.
Timer should run One time but with different Intervals
In the link you provided you will find the answer if you try little harder.
For a repeating task:
new Timer().scheduleAtFixedRate(task, after, interval);
For a single run of a task:
new Timer().schedule(task, after);
So what you need to do is to maintain temporary variable to keep track of number of clicks and you can use second method like
For a repeating task:
new Timer().scheduleAtFixedRate(task, after, interval);
For a single run of a task:
new Timer().schedule(task, after * numberOfTimeBtnClked);
You have to pass the TimerTask
method instead of task in that method.
**For updating your textview use below code and forget about whatever I have written above **
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//run in an interval of 1000ms
timer.schedule(timerTask, 0, 1000); //
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
timerSince++; //global integer variable with default value 0
if(timerSince == 5 * numberOfBtnClick){
//call your method
timer.cancel;
timerSince = 0;
}else{
//textView.setText(((5 * numberOfBtnClick)-timerSince)+" second left");
}
});
}
};
}
}
On event start button click call:
startTimer();
How do you use a TimerTask to run a thread?
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.
Related Topics
Android Webview "Tel:" Links Show Web Page Not Found
Android - Get Text Out of Webview
Android Action Bar Not Showing Overflow
Launch Android Application Without Main Activity and Start Service on Launching Application
No Activity Found to Handle Intent:Android.Intent.Action.View
How to Shrink the Drawable on a Button
Toolbar Navigation Icon Never Set
How to Clear an Imageview in Android
Android Onclick in Xml VS. Onclicklistener
How to Make a Phone Call Programmatically
Opening Android Settings Programmatically
How to Customize Item Background and Item Text Color Inside Navigationview
Pick Any Kind of File via an Intent in Android
Mediaplayer Stutters at Start of Mp3 Playback
Is an Android Service Guaranteed to Call Ondestroy()