Created
June 18, 2011 01:44
-
-
Save codeswimmer/1032711 to your computer and use it in GitHub Desktop.
Android: lightweight means of performing a task on a periodic basis (i.e. polling every 500 milliseconds).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.codeswimmer.android.executor; | |
| import java.util.concurrent.Executor; | |
| import android.os.Handler; | |
| import android.os.SystemClock; | |
| public class PeriodicExecutor implements Executor { | |
| private static final Handler poller = new Handler(); | |
| private Runnable command; | |
| private long period; | |
| public PeriodicExecutor(long period) { | |
| this.period = period; | |
| } | |
| @Override | |
| public void execute(Runnable command) { | |
| this.command = command; | |
| poller.postDelayed(executor, period); | |
| } | |
| private final Runnable executor = new Runnable() { | |
| @Override | |
| public void run() { | |
| long uptimeMillis = SystemClock.uptimeMillis(); | |
| command.run(); | |
| poller.postAtTime(this, uptimeMillis + PeriodicExecutor.this.period); | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment