Android : Do something at x minutes then again every n-th minute

on Friday, December 12, 2014


I have a global variable that tracks when an ad on a particular screen is shown.


I need an ad to show if the user visits this screen after 2 minutes or more, then again after every 4 minutes. So show the ad again if the user visits the screen 6 minutes or more after that and then 10 minutes, 14 etc


I have the following code, it works fine if the user visits the screen at minutes 2,6,10,14 etc however it does not work if the user misses these minutes. for example if an ad was shown at minute 2 but the user didn't come back until minute 8, the ad for minute 6 is missed.


How can I cater for this with what i have so that if the user comes back outside of these minutes, the ad for them will still be shown?



long start = adStartTime;
long now = new Date().getTime();
int minsElapsedSinceLastAd = (int) ((now - start) / 1000 / 60);
showAd = previousRunMin !=minsElapsedSinceLastAd&& minsElapsedSinceLastAd % 4 == 2;
if (showAd) {
showAd();
...
previousRunMin = minsElapsedSinceLastAd;

}


previousRunMin is there so that if the user comes back on the same minute, the ad is not shown


0 comments:

Post a Comment