0

I'm in the process of setting up ads in Android and have been successful with adding Interstitials and Banner ads. I'm now in the process of setting up removal of the ads with a subscription and a reward.

The ad provider I use offer a reward for watching a video, I'd like to disable ads for 24 hours after a user watches one of these videos. What would be the best way of achieving this?

My initial thought would be to store a boolean and then revert it after 24 hours.

TL;DR - How do I change the value of a variable for 24 hours using shared preferences?

MM1010
  • 591
  • 1
  • 9
  • 26
  • 1
    Possible duplicate of [Android timer? How-to?](https://stackoverflow.com/questions/4597690/android-timer-how-to) – Simion Aug 13 '18 at 17:32
  • 3
    Setting a variable's value and somehow arranging for it to be changed again later is possible, but fragile. A design that does not require the subsequent change would be much preferable. For example, instead of a `boolean disableAds` variable, how about a `Date disableAdsUntil` variable? – John Bollinger Aug 13 '18 at 17:33
  • 1
    Certainly sounds more reliable. Is that something that can be put in shared prefs or is it handled differently? – MM1010 Aug 13 '18 at 17:39
  • Your thought **My initial thought would be to store a boolean and then revert it after 24 hours.** looks fine to me. What is the issue with this approach ? – Krishna Sharma Aug 13 '18 at 18:23
  • @KrishnaSharma Some solutions are better than others. Using a boolean requires logic to reset it in 24 hours. How do you determine if it has been 24 hours? looks like you also need a SystemTimeInMilli to compare for 24 hour time frame. Now you have 2 variables to reset and maintain. JohnBollingers answer only requires a simple check to a date. If date is true get new 24 hour date. – doubleA Aug 13 '18 at 19:01
  • @doubleA you should trigger alarm for 24 hour – Krishna Sharma Aug 13 '18 at 19:02
  • Now you are waking your app or triggering some logic when there does not need to be any. It is not wrong. It is just more effort for a simple task. – doubleA Aug 13 '18 at 19:03

3 Answers3

1

you can save the boolean value (maybe with some identification) in somewhere (SharedPreference or database) with the timestamp value (that shows the time you saved the boolean value) and when the app starts you can check the timestamp you saved with current time your object might be like below:

public class YourObject {

    private int id;
    private boolean show;
    private long time;
}

you get current millisecond time with:

System.currentTimeMillis(); 

(I'm not sure the id is useful or not)

1

Something like this should work.

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
long hideAdUntil = sp.getLong(HIDE_AD_UNTIL_KEY, 0);
if(System.currentTimeMillis() > hideAdUntil) {
    //Display Ad
}

At the end of your user watching a video edit your shared preference value to reflect 24 hour time frame.

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
sp.edit().putLong(HIDE_AD_UNTIL_KEY, System.currentTimeMillis() + 86,400,000).apply();
doubleA
  • 2,446
  • 22
  • 45
0

The problem with reverting a boolean after 24 hours is that your app may not be running in 24 hours, or the device may be turned off. When an ad is done playing store the current date and time. Then, every time you are about to show an ad check if the current day and time is 24 hours greater than the stored time. Only if it is, show the ad, and when its done store the new time.

Raymond Mutyaba
  • 950
  • 1
  • 9
  • 14