Execute method in all Activitys according to an interval

0

I need to run a method every 20 seconds, this method has a condition, if it is false it will send an AlertDialog. But this method should be run independent of the Activity that is in the foreground, how to do?

The method should run only if the application is in the foreground.

    
asked by anonymous 28.02.2015 / 05:38

1 answer

1

If you want the 20-second counter to be restarted with each screen change, then do the activities extend a Base Activity, whose onCreate () method starts the 20-second counter thread (only if savedInstanceState == null, of course, to avoid resetting the counter in case of screen rotation). At the end of 20 seconds, he calls AlertDialog through a Handler.

However, if you want the counter to be global for the application, so that the user stays 10 seconds on a screen and when switching screens are only 10 seconds away to appear AlertDialog, do as follows: ActivityBase should register a broadcast receiver in onResume () and "unregister" it onPause (). Start a counter thread in a separate class, and when the counter runs out, launch a broadcast, which the activity currently in the foreground is ready to receive.

    
28.02.2015 / 22:59