Do something else inside a timer?

Tuesday, November 3, 2015

I am making an app that has a timer inside a service, and the timer runs in the background. I want to put a log in the logcat when 10 seconds are up. The timer does a different task at 20 seconds, but I want it to also log when 10 seconds are up . Here is my attempt:



public class TwentySeconds extends Service {
final String TAG = "MyCountdown";

CountDownTimer cdt;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Log.v(TAG, "In start command");

cdt = new CountDownTimer(20000, 1000) {

@Override
public void onTick(long millisUntilFinished) {
long timeRemaining = 20000 - millisUntilFinished;
if(timeRemaining % 1000 == 0){
Log.v(TAG, "Time remaining" +(timeRemaining % 1000) + " seconds left");
}
}
@Override
public void onFinish() {
Log.v(TAG, "Finished");

Intent intent = new Intent(TwentySeconds.this,GameOver.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.v(TAG, "About to start activity");
startActivity(intent);
}
};
cdt.start();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}


The logic for the code seems great, but for some reason, it is not displaying. Please explain how I can achieve the timer to display a message after ten seconds while also doing what it's supposed to do at the end.
Thanks so much for all of your time, please let me know what you think!



{Rich}

0 comments:

Post a Comment