Corona SDK : Admob Controller for Android

Corona SDK : Admob Controller for Android

Test on:
- Mac OS X El Capitan
- Asus Zenfone 4.5
- Android SDK
- Corona SDK [download] https://coronalabs.com/

[Free sample source code]
Apk file: 
Source code:

[Pre-require]
If you don't have your project please create simple test project from this link first

Corona SDK : Getting start for Android
http://gamedevtodied.blogspot.com/2016/04/corona-sdk-getting-start-for-android.html

[Admob implement]
1. Open your "main.lua" and add this code after last line. I'm create textfield for debug any event in this project, you'll be known what happen in your app, the second is Ads implement for Banner and Interstitial for this sample is dummy id, you need to change later when publish app,
      i'm separate ads into two objects for track difference event and special isBannerShow for only Banner, this third part use for button implementation as show/hide banner, show interstitial and reload interstitial because when we close interstitial, the ads was dispose then we need to reload it.

[start - code]

-- debugger
txtDebug = native.newTextField( display.contentWidth * 0.5, 100, display.contentWidth, 35 )
txtDebug.align = "center"
txtDebug:setTextColor( 1, 0.5, 0 )
txtDebug.text = "Ad Debug"

-- Ads Implement
uidBanner = "ca-app-pub-3940256099942544/6300978111"
uidInters = "ca-app-pub-3940256099942544/1033173712"
adsBanner = require( "ads" )
adsInterstitial = require( "ads" )
isBannerShow = false
-- Optional table containing targeting parameters
targetingParams = { tagForChildDirectedTreatment = true }

-- For button widget
local widget = require "widget"

[end - code]

Let's see sample image, your code similar to be like this.


2. Create button for show/hide banner and function when button event was called.

[start - code]

-- Function to handle button Banner events
local function handleButtonBannerEvent( event )
    if ( "ended" == event.phase ) then
        if ( isBannerShow == false ) then
            print( "Banner was pressed and released" )
            txtDebug.text = "show banner"
            adsBanner.show( "banner", { x=0, y=0, targetingOptions=targetingParams} )
            isBannerShow = true;
        else
            txtDebug.text = "hide banner"
            adsBanner.hide()
            isBannerShow = false
        end 

    end
end
local btnBanner = widget.newButton{
label="Test Banner",
labelColor = { default={255}, over={128} },
default="button.png",
over="button-over.png",
width=154, height=40,
onRelease = handleButtonBannerEvent -- event listener function
}
btnBanner.x = display.contentWidth * 0.5
btnBanner.y = display.contentHeight - 300

[end - code]

Let's see sample image, your code similar to be like this.


3. Create button for show interstitial and function when button event was called. For interstitial it can't be hide by ads.hide() method and we not known when it closed, so i'm making check is it finish load ads condition, if not just reload.

[start - code]

-- Function to handle button Interstitial events
local function handleButtonIntersEvent( event )

    if ( "ended" == event.phase ) then
        if ( adsInterstitial.isLoaded( "interstitial" ) ) then
            txtDebug.text = "show Interstitial"
            print( "Interstitial was pressed and released" )
            adsInterstitial.show( "interstitial", { x=0, y=0, targetingOptions=targetingParams} )
        else
            txtDebug.text = "Inters Not Ready Try Load"
            adsInterstitial.load( "interstitial", { testMode=true } )
        end
    end
end
local btnInters = widget.newButton{
label="Test Interstitial",
labelColor = { default={255}, over={128} },
default="button.png",
over="button-over.png",
width=154, height=40,
onRelease = handleButtonIntersEvent -- event listener function
}
btnInters.x = display.contentWidth * 0.5

btnInters.y = display.contentHeight - 250

[end - code]

Let's see sample image, your code similar to be like this.


4. Create button for reload interstitial and function when button event was called. This is direct call method load interstitial ads with simple button then you can press show interstitial after you pressed this button.

[start - code]

-- Function to handle Reload Interstitial events
local function handleReloadIntersEvent( event )

    if ( "ended" == event.phase ) then
        txtDebug.text = "Re-Load Interstitial"
        adsInterstitial.load( "interstitial", { testMode=true } )
    end
end
local btnLoadInters = widget.newButton{
label="Reload Interstitial",
labelColor = { default={255}, over={128} },
default="button.png",
over="button-over.png",
width=154, height=40,
onRelease = handleReloadIntersEvent -- event listener function
}
btnLoadInters.x = display.contentWidth * 0.5

btnLoadInters.y = display.contentHeight - 200

[end - code]

Let's see sample image, your code similar to be like this.


5. Create event listener function for both banner and interstitial, then call init ads and special one with load ads for interstitial when launch app and you can press show interstitial on starting app.

[start - code]

-- Function to handle initialize of Admob
local function adBannerListener( event )
    if ( event.isError ) then
        -- Failed to receive an ad
        print( "Error init Banner " + event.isError )
        txtDebug.text = "Error init Banner " + event.isError
    end
end

local function adInterstitialListener( event )
    if ( event.isError ) then
        -- Failed to receive an ad
        print( "Error init Interstitial " + event.isError )
        txtDebug.text = "Error init Interstitial " + event.isError   
    end
end
-- Call initialize of Admob
adsBanner.init( "admob", uidBanner, adBannerListener )
adsInterstitial.init( "admob", uidInters, adInterstitialListener )

adsInterstitial.load( "interstitial", { testMode=true } )

[end - code]

Let's see sample image, your code similar to be like this.


6. Open your "build.settings" and add this code to your file

[start - code]

    --
    -- Admob Plugin
    --
    plugins =
    {
        ["plugin.google.play.services"] =
        {
            publisherId = "com.coronalabs",
            supportedPlatforms = { iphone=false, android=true }
        },

    },

[end - code]


Let's see sample image, your code similar to be like this.


and don't forget to check in this file having below code included?, because your ads need permission for android device to receive ads.

[start - code]

--
-- Android Section
--
android =
{
usesPermissions =
{
"android.permission.INTERNET",
           "android.permission.ACCESS_NETWORK_STATE",
},
},

[end - code]

[Test Run]
1. Goto Corona Simulator, "File > Build > Android..."


2. Build setting dialog for android will open, check your detail and edit "Package" as you want and "After Build" choose "Copy to device and launch" (It's maybe not launch but success installed).


3. Click "Build" and wait a while for processing build and install on android device.



4. When it's completed, you should see dialog "Device Installation Complete" (It's maybe not launch on device, take a look by yourself).


5. Take a look on your android device, (find grey small "icon" and name as your configuration) and launch it.


[Free sample source code]
Apk file: 
Source code: 

It's work :)

Comments

Post a Comment