Created
August 3, 2015 20:06
-
-
Save Marchuck/46331f123838c7ecc8e5 to your computer and use it in GitHub Desktop.
how to invoke the same method in a loop
This file contains 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
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