Reputation: 7571
I have a service with a timer in the background. This timer executes a certain task after 20 seconds pass. I want the same timer to execute another task, but in a different amount of seconds. This would be easy by launching both services at the same time, but I am trying to learn how I can do it this other way.
I tried this, but it isn't showing up:
cdt = new CountDownTimer(20000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Log.v(TAG, /*"Time remaining " +*/ (millisUntilFinished / 1000) + " seconds left");
if(millisUntilFinished / 10000 == 0){ //TEN SECONDS
Log.v(TAG, (millisUntilFinished % 1000) + " seconds left");
}
}
@Override
public void onFinish() {
Log.v(TAG, "Finished");
Now, I should be getting two different log messages at ten seconds. Instead, I only get one. Here is my logCat:
The arrow indicates where there should have been another log message.
I am lost. I would really appreciate any feedback (positive or negative)! Thank you so much for all of your help.
Upvotes: 1
Views: 611
Reputation: 2580
Your log is not displaying because:
if(millisUntilFinished / 10000 == 0){ //TEN SECONDS
Log.v(TAG, (millisUntilFinished % 1000) + " seconds left");
}
you want it to display when you have 10000 millisUntilFinished but 10000/10000 = 1 not 0.
it should be:
if(millisUntilFinished / 10000 == 1) || if(millisUntilFinished % 10000 == 0)
or simply:
if(millisUntilFinished == 10000)
Upvotes: 0
Reputation: 11497
As you said yourself, you could launch both services at the same time.
Or inside the first service, you could pass an Intent
with some parameters to inform the new amount of time and then call startService()
.
Intent it = new Intent(getApplicationContext(), YourService.class);
it.putExtra("Time", timeInMillis);
startService(it);
I mean, modify your Service
to get an extra from the calling Intent
, something like this:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
long time = intent.getLongExtra("Time",0);
// Do your ticking here, based on that variable.
}
//...
}
Edit: Let's say you want to make your timer run first at 5 seconds, then at 10, 15, and so on.
Once you finish your timer, you could pass the Intent
to the new Service
/Timer adding the seconds you want. Like:
it.putExtra("Time", timeInMillis + 5000);
Upvotes: 1
Reputation: 1412
Use this type.
cdt = new CountDownTimer(10000, 1000) { //finished on 10 Seconds
@Override
public void onTick(long millisUntilFinished) {
Log.v(TAG, /*"Time remaining " +*/ (millisUntilFinished / 1000) + " seconds left");
}
@Override
public void onFinish() {
Log.v(TAG, "First 10 Seconds Finished");
LoadNextCountdown(); // call the Next Countdown method
}
}
.start();
//Method for Next Countdown
public void LoadNextCountdown()
{
cdt2=new CountDownTimer(10000,1000) { //finished on 10 Seconds
@Override
public void onTick ( long millisUntilFinished){
Log.v(TAG, /*"Time remaining " +*/ (millisUntilFinished / 1000) + " seconds left");
}
@Override
public void onFinish () {
Log.v(TAG, "Next 10 Seconds Finished");
}
} .start();
}
Happy Coding :)
Upvotes: 0