Skip to content

Instantly share code, notes, and snippets.

@Marchuck
Created August 3, 2015 20:06
Show Gist options
  • Save Marchuck/46331f123838c7ecc8e5 to your computer and use it in GitHub Desktop.
Save Marchuck/46331f123838c7ecc8e5 to your computer and use it in GitHub Desktop.
how to invoke the same method in a loop
public class Loop {
private static boolean isStarted = false;
private static Handler handler = new Handler();
private static ConcreteActivity activity;
public static boolean isActive = true;
private static Runnable stepTimer = new Runnable() {
@Override
public void run() {
if (isActive) {
activity.LoopAction();
handler.postDelayed(this, 2000); //LoopAction() is invoked every 2 seconds
}
}
};
public static void start(ConcreteActivity view) {
if (!isStarted) {
handler.postDelayed(stepTimer, 1000); //start looping after 1 second of delay
isStarted = true;
}
activity = view;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment