questions about Admob

hi, yes, my main.lua contains my main menu code. When a user hits “menu” from from one of the flashcard screens, it calls a function in main.lua to display the main menu again. At the top of the main menu code is a call to the “ShowAnAd()” function in the OP above.

So would it be better to have

local function adListener( event ) [...] end admob.init( adListener,  {appId="XXX", testMode=true }  )

at the very beginning of main.lua (called only once), and then do the following in a different .lua file, perhaps within the ‘return to menu’ button listener within a flashcard module?

if ( admob.isLoaded( "interstitial" ) ) then admob.show( "interstitial" ) end

I’m not clear why I can’t immediately follow admob.init by admob.isloaded. Does the init function need more time to complete before we call isLoaded? Thanks.

As I said above, it takes time for the plugin to initialize. It then takes more time to load an ad. To prevent your app’s UI from blocking, many network-based API’s behave like this. That is they return immediately, process the task in the background and notify the foreground via an event when it finishes.

Today’s computers and mobile devices execute millions of instructions per second and the time between .init() finishing and the finishing of your main.lua is fractions of a second later, which isn’t nearly enough time for network operations to finish.

Rob

OK I have admob.init and its listener once only, right at the start of main.lua.

The calls to show the ad are elsewhere.

I’m still finding that I’m hardly getting any ads (I’m in test mode). The behaviour I’m seeing at the moment is that after I do a build and copy to device and launch, the very first ad call works … but then every subsequent time I hit the menu button (and call an ad) there is no ad (I tried around 30 times in a row). The only time I saw anything ad-related in the console was when the ad displayed. On the subsequent failed calls it displays nothing.

I put a print statment into admob.isloaded to see if it is or not. It only ever is the first time it’s called

I’m finding this kind of confusing at the moment: the init listener calls admob.load. Does this load just one ad, or a whole bunch? Presumably a bunch, if we’re only supposed to call admob.init once in the app. Yet right now it’s behaving like it’s just fetching one ad, and that’s it.

Are you calling ad.load() each time?

Rob

I only have “admob.load” once. It’s in the listener for admob.init:

        if ( event.phase == "init" ) then  -         admob.load("interstitial" .... ...         end

That’s the one and only place i had it.

So I tried just now to add admob.load(…params…) right before

    if ( admob.isLoaded( "interstitial" ) )         admob.show( "interstitial" ) 

but I’m not getting any ads now!

Not sure if it’s helful but here’s a bit of console output. The “not loaded” is from a print statement I added showing that admob.isLoaded is false. “Inside showanad” in another print statment I added.

Aug 20 04:16:14.743 XT1032: inside showanad
Aug 20 04:16:14.743 XT1032:
Aug 20 04:16:14.744 plugin.admob: Test mode active for device ‘C936582D06E66C0D1D3C68DC8610E229’
Aug 20 04:16:14.872 XT1032: not
Aug 20 04:16:14.873 loaded
Aug 20 04:16:14.893 XT1032:
Aug 20 04:16:14.893  getRunningAppProcesses: caller 10069 does not hold REAL_GET_TASKS; limiting output
Aug 20 04:16:14.903 XT1032: get
Aug 20 04:16:14.904 RunningAppProcesses: caller 10122 does not hold REAL_GET_TASKS; limiting output
Aug 20 04:16:14.907 XT1032: getRunni
Aug 20 04:16:14.907 ngAppProcesses: caller 10069 does not hold REAL_GET_TASKS; limiting output
Aug 20 04:16:16.646 XT1032: {
                    XT1032:   “data”:"{“adUnitId”:“XXXXXXXXXX”}",
                    XT1032:   “name”:“adsRequest”,
                    XT1032:   “phase”:“loaded”,
Aug 20 04:16:16.647 XT1032:   “provider”:“admob”,
                    XT1032:   “type”:“interstitial”,
                    XT1032:   “isError”:false
                    XT1032: }
Aug 20 04:16:37.573 XT1032: in
Aug 20 04:16:37.573 side showanad
                    XT1032: plugin.admob: Test mode active for device ‘C936582D06E66C0D1D3C68DC8610E229’
Aug 20 04:16:37.607 XT1032: getRu
Aug 20 04:16:37.607 nningAppProcesses: caller 10122 does not hold REAL_GET_TASKS; limiting output
Aug 20 04:16:37.618 XT1032: not loa
Aug 20 04:16:37.618 ded

 

You should be getting more events than that. When you call admob.show(“interstitial”), there should be an event or two coming into the listener. For instance, there should be a “closed” phase when an interstitial is closed.  There should be a “displayed” phase when the ad was shown. 

You can, when you’ve gotten either the “closed” phase or possibly the “displayed” phase, call admob.load() to load the next interstitial ad. That will preload the next ad for the next time you need to show it. You could potentially call admob.show() and immediately call admob.load(), but I’m not 100% sure of how AdMob will behave in that case.

Rob

I’ve seen the displayed and closed events in my output.

Where would I add this code? Do I need to add more cases to the admob.init’s listener? Something like this?

    if ( event.phase == "init" ) then     admob.load("interstitial",{adUnitId }) elseif (event.phase == "displayed) then admob.load(...... elseif (event.phase == "closed") admob.load(..... .....      end

That code would go in your adListener function. You shouldn’t need it anywhere else. Also, you probably don’t need to call admob.load() for both “displayed” and “closed”, pick the one that works for you.

Rob

OK seems to be working now, thanks so much for all the help.