Showing posts with label How to stop the timer after certain time?. Show all posts
Showing posts with label How to stop the timer after certain time?. Show all posts

Tuesday, April 9, 2013

[android help] How to stop the timer after certain time?

java - How to stop the timer after certain time? - Stack Overflow




















I have an android application which has a timer to run a task:



time2.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
sendSamples();
}
}, sampling_interval, sending_interval);


let's say sampling_interval is 2000 and sending_interval is 4000. So in this application I send some reading values from a sensor to the server. But I want to stop the sending after 10000 (10 seconds) what should I do?


























Check this code:



private final static int DELAY = 10000;
private final Handler handler = new Handler();
private final Timer timer = new Timer();
private final TimerTask task = new TimerTask() {
private int counter = 0;
public void run() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();
}
});
if(++counter == 4) {
timer.cancel();
}
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer.schedule(task, DELAY, DELAY);
}






















try



time2.scheduleAtFixedRate(new TimerTask() {
long t0 = System.currentTimeMillis();
@Override
public void run() {
if (System.currentTimeMillis() - t0 > 10 * 1000) {
cancel();
} else {
sendSamples();
}
}
...



















lang-java







.

stackoverflow.comm

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...