Is there in Android an efficent way to run a task only for a limited time? Currently I use a Handler with onPostDelayed() and check each run if the current timestamp is higher than my predefined timestamp.
I'm searching for a solution like CountdownTimer but without ticks. I only want define a start and endtime and my task should perform in this period.
If I want to run a task for a specific time I save the time it starts into a 'long' like so:
And then simply check how long it has been since the task started (assuming it is a loop of some kind) while it is running so if I wanted to run something for 5 seconds: