Execute method on all Activitys according to a range

Asked

Viewed 50 times

0

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

The method should be executed only if the application is in the foreground.

1 answer

1


If you want the 20-second counter to be restarted at each screen change, then do the activities extend an Activitycase, in whose onCreate() method the 20-second counter thread is started (only if savedInstanceState == null, of course, to avoid restarting the counter in case of screen rotation). Hit the 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 change screen only 10 seconds to appear Alertdialog, do so: Activityto register a broadcast receiver in onResume() and "unregister" in onPause(). Start a thread counter in a separate class and, when the counter is over, launch a broadcast, which the currently foreground activity will be ready to receive.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.