Question

Context: Using the latest Google Play admob... I have an interstitial ad in an activity, with an adListener.

What I am trying to accomplish: When an ad fails to load (either because device is using adblock, or device doesn't have access to network), I would like for a custom activity to launch (which I have setup to look like a custom ad).

What I am currently using as code to accomplish this:

interstitial.setAdListener(new AdListener()
    {
        @Override
        public void onAdLoaded()
        {
            displayInterstitial();
            super.onAdLoaded();
        }

        @Override
        public void onAdFailedToLoad(int errorCode)
        {
            Intent intent = new Intent(getApplicationContext(),
                    FailToLoadActivity.class);
            startActivity(intent);
            super.onAdFailedToLoad(errorCode);
        }

    });

I have also tried adding those Intent... lines to the displayInterstitial() method:

public void displayInterstitial()
{
    if (interstitial.isLoaded())
    {

        interstitial.show();
    }
    else
    {
        Intent intent = new Intent(getApplicationContext(),
                FailToLoadActivity.class);
        startActivity(intent);
    }
}

Now, instead of my custom ad showing right away when the request fails, it seems to appear ~30 seconds after. It does this even when the activity is destroyed.

How can I get this custom ad to show as soon as the request first fails, and not do it at all when the activity is destroyed?

/e I've noticed an entry in the logcat: Scheduling ad refresh 60000ms from now, I suppose changing this to 5000ms would solve my problem (again, this is a theory)... Is this something I can change?

Also, I would like to guarantee that no ad (my custom ad, or network served ad) show when the user exits the activity (to prevent any out-of-app invasive pop-ups from assaulting my users)

Was it helpful?

Solution

Get rid of your AdListener.

You never want to display an interstitial ad as soon as it has been received, nor your custom one as soon as the interstitial cannot be served.

Instead call #displayInterstitial at a natural break point in your app. This will ensure that you either show an interstitial or show your custom ad. And you won't get it displaying when your Activity has been destroyed.

The other approach you could take is to setup an inhouse ad and set that as the fallback option in your mediation flow. But I have never attempted to make that happen. And besides you wanted this to work even when the user was offline, so top approach is best.

OTHER TIPS

Based on William's suggestion:

NOTE: I wanted the interstitial to pop-up when the user clicks a specific button in my main menu (of a live wallpaper configuration activity)

1 - I create and request a load for the interstitial in onCreate(), and did NOT instantiate an AdListener.

2 - Changed displayInterstitial() to:

public void displayInterstitial()
{
    if (interstitial.isLoaded())
    {
        interstitial.show();
    }
    else
    {
        Intent intent = new Intent(getApplicationContext(),
                FailToLoadActivity.class);
        startActivity(intent);
    }
}

3 - Changed my onClick method from directly launching the new activity to:

public void onClickFX(View v)
{
    displayInterstitial();
    boolFX = true;
}

4 - Overrode onResume() as follows:

@Override
protected void onResume()
{
    if (boolFX)
    {
        boolFX = false;
        Intent intent = new Intent(getApplicationContext(),
                SpecialEffects.class);
        startActivity(intent);
    }
    super.onResume();
}

This works perfectly!

So, thanks William for pointing me in the right direction!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top