Search code examples
androidadmobadsinterstitial

How to change the frequency of interstitial ads


Currently, my home page has 10 buttons and each button has an interstitial attached.

    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
    } else {
        Intent intent = new Intent(this, magic.class);
        startActivity(intent);
    }

Loading an interstitial for each new intent is a little bit overboard though. How can I manipulate the interstitials to load after every 5 intents?


Solution

  • Create an integer

    private int showbigad =0;
    

    In OnCreate...

    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
    String restoredText = prefs.getString("text", null);
    if (restoredText != null) {
    int advalue = prefs.getInt("showad", 0); 
    showbigad=advalue;
    }
    

    Now with each click we add to showbigad

    EACHONEOFYOURBUTTONS.onClick....{
    showbigad =showbigad+1;
    SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
    editor.putInt("showad", showbigad);
    editor.commit();
    }});
    

    Now if buttons have been clicked more than 4 times the interstitial ad will show

    if (mInterstitialAd.isLoaded()&& showbig>4) {
        mInterstitialAd.show();
    } else {
        Intent intent = new Intent(this, magic.class);
        startActivity(intent);
    }