Totalys
Totalys

Reputation: 465

Admob in Corona is not running at the phone

I created an example app with Corona and I tried to implement admob functions.

I ran the admob example and it worked. But when I combined it with this sample code below it stopped working.

At the emulator it runs normally. But when I upload the apk to my phone, I got a black screen.

Not an error, but a weird black screen.

Do you know what am I doing wrong?

-----------------------------------------------------------------------------------------
--
-- menu.lua
--
-----------------------------------------------------------------------------------------

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

-- include Corona s "widget" library 
local widget = require "widget"

--------------------------------------------

-- forward declarations and other locals
local playBtn
local optsBtn
local helpBtn

-- ************* ADMOB *********************
-- Hide the status bar
display.setStatusBar( display.HiddenStatusBar )

-- The name of the ad provider.
local provider = "admob"

-- Your application ID
local appID = "a1522213c297e5a"

-- Load Corona 'ads' library
local ads = require "ads"

--------------------------------------------------------------------------------
-- Setup ad provider
--------------------------------------------------------------------------------

-- Create a text object to display ad status
local statusText = display.newText( "", 0, 0, native.systemFontBold, 22 )
statusText:setTextColor( 255 )
statusText:setReferencePoint( display.CenterReferencePoint )
statusText.x, statusText.y = display.contentWidth * 0.5, 160

local showAd

-- Set up ad listener.
local function adListener( event )
    -- event table includes:
    --      event.provider
    --      event.isError (e.g. true/false )

    local msg = event.response

    -- just a quick debug message to check what response we got from the library
    print("Message received from the ads library: ", msg)

    if event.isError then
        statusText:setTextColor( 255, 0, 0 )
        statusText.text = "Error Loading Ad"
        statusText.x = display.contentWidth * 0.5

        showAd( "banner" )
    else
        statusText:setTextColor( 0, 255, 0 )
        statusText.text = "Successfully Loaded Ad"
        statusText.x = display.contentWidth * 0.5
    end
end

-- Initialize the 'ads' library with the provider you wish to use.
if appID then
    ads.init( provider, appID, adListener )
end

--------------------------------------------------------------------------------
-- UI
--------------------------------------------------------------------------------

-- initial variables
local sysModel = system.getInfo("model")
local sysEnv = system.getInfo("environment")

-- create a background for the app
--local backgroundImg = display.newImageRect( "background.jpg", display.contentWidth, display.contentHeight )
--backgroundImg:setReferencePoint( display.TopLeftReferencePoint )
--backgroundImg.x, backgroundImg.y = 0, 0
statusText:toFront()

-- Shows a specific type of ad
showAd = function( adType )
    local adX, adY = display.screenOriginX, display.screenOriginY
    statusText.text = ""
    ads.show( adType, { x=adX, y=adY } )
end

-- if on simulator, let user know they must build for device
if sysEnv == "simulator" then
    local font, size = native.systemFontBold, 22
    local warningText = display.newRetinaText( "Please build for device or Xcode simulator to test this sample.", 0, 0, 290, 300, font, size )
    warningText:setTextColor( 255 )
    warningText:setReferencePoint( display.CenterReferencePoint )
    warningText.x, warningText.y = display.contentWidth * 0.5, display.contentHeight * 0.5
else
    -- start with banner ad
    showAd( "interstitial" )
end

-- ************* FIM ADMOB *****************


-- 'onRelease' event listener for playBtn
local function onPlayBtnRelease()

    -- go to level1.lua scene
    storyboard.gotoScene( "level1", "fade", 500 )

    return true -- indicates successful touch
end

-- Options
local function onOptionsBtnRelease()

    -- go to level1.lua scene
    storyboard.gotoScene( "options", "fade", 500 )

    return true -- indicates successful touch
end

-- Options
local function onHelpBtnRelease()

    -- go to level1.lua scene
    storyboard.gotoScene( "help", "fade", 500 )

    return true -- indicates successful touch
end


-----------------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
-- 
-- NOTE: Code outside of listener functions (below) will only be executed once,
--       unless storyboard.removeScene() is called.
-- 
-----------------------------------------------------------------------------------------

-- Called when the scene s view does not exist:
function scene:createScene( event )
    local group = self.view

    -- display a background image
    local background = display.newImageRect( "background.jpg", display.contentWidth, display.contentHeight )
    background:setReferencePoint( display.TopLeftReferencePoint )
    background.x, background.y = 0, 0

    -- create/position logo/title image on upper-half of the screen
    local titleLogo = display.newImageRect( "title.png", 264, 42 )
    titleLogo:setReferencePoint( display.CenterReferencePoint )
    titleLogo.x = display.contentWidth * 0.5
    titleLogo.y = display.contentHeight / 5

    -- create a widget button (which will loads level1.lua on release)
    playBtn = widget.newButton{
        label="Play Now",
        labelColor = { default={255}, over={128} },
        defaultFile="button.png",
        overFile="button-over.png",
        width=154, height=40,
        onRelease = onPlayBtnRelease    -- event listener function
    }
    playBtn:setReferencePoint( display.CenterReferencePoint )
    playBtn.x = display.contentWidth*0.5
    playBtn.y = display.contentHeight * 2 / 5

    -- button for options (options.lua)
    optsBtn = widget.newButton{
        label="Options",
        labelColor = { default={255}, over={128} },
        defaultFile="button.png",
        overFile="button-over.png",
        width=154, height=40,
        onRelease = onOptionsBtnRelease -- event listener function
    }
    optsBtn:setReferencePoint( display.CenterReferencePoint )
    optsBtn.x = display.contentWidth*0.5
    optsBtn.y = display.contentHeight * 3 / 5

    -- button for options (help.lua)
    helpBtn = widget.newButton{
        label="Help",
        labelColor = { default={255}, over={128} },
        defaultFile="button.png",
        overFile="button-over.png",
        width=154, height=40,
        onRelease = onHelpBtnRelease    -- event listener function
    }
    helpBtn:setReferencePoint( display.CenterReferencePoint )
    helpBtn.x = display.contentWidth*0.5
    helpBtn.y = display.contentHeight * 4 / 5

    -- all display objects must be inserted into group
    group:insert( background )
    group:insert( titleLogo )
    group:insert( playBtn )
    group:insert( optsBtn )
    group:insert( helpBtn )
end

-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
    local group = self.view

    -- INSERT code here (e.g. start timers, load audio, start listeners, etc.)

end

-- Called when scene is about to move offscreen:
function scene:exitScene( event )
    local group = self.view

    -- INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)

end

-- If scene s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
    local group = self.view

    if playBtn then
        playBtn:removeSelf()    -- widgets must be manually removed
        playBtn = nil
    end
end

-----------------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
-----------------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene s view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched whenever before next scene s transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

-----------------------------------------------------------------------------------------

return scene

Upvotes: 0

Views: 2376

Answers (2)

Totalys
Totalys

Reputation: 465

Well, just adding this, it worked:

-- ************* ADMOB *********************
-- Hide the status bar
display.setStatusBar( display.HiddenStatusBar )

-- The name of the ad provider.
local adNetwork = "admob"

-- Your application ID
local appID = "a1522213c297e5a"

-- Load Corona 'ads' library
local ads = require "ads"

-- Initialize the 'ads' library with the provider you wish to use.
if appID then
 ads.init( adNetwork, appID )
end

 -- initial variables
local sysModel = system.getInfo("model")
local sysEnv = system.getInfo("environment")
local bgW, bgH = 320, 480

if appID then
    local adX, adY = display.contentCenterX, 0
    local halfW = display.contentWidth * 0.5
    local font, size = "Helvetica-Bold", 16
    if sysEnv == "simulator" then
        local warningText2 = display.newText( "Please build for device ", adX, adY, font, size )
        local warningText3 = display.newText( "to test this sample code.", adX, adY, font, size )
        warningText2:setTextColor( 255, 255, 255)
        warningText3:setTextColor( 255, 255, 255)
        warningText2:setReferencePoint( display.CenterReferencePoint )
        warningText3:setReferencePoint( display.CenterReferencePoint )
        warningText2.x, warningText2.y = halfW, 0
        warningText3.x, warningText3.y = halfW, 16
    else
        ads.show( "banner", { x=adX, y=adY} )
    end
else
    -- If no appId is set, show a message on the screen
    local warningText1 = display.newText( "No appID has been set.", 0, 105, font, size )
    warningText1:setTextColor( 255, 255, 255)
    warningText1:setReferencePoint( display.CenterReferencePoint )
    warningText1.x = halfW
end

And, at the build.settings file:

add this:

plugins =
    {
        -- key is the name passed to Lua's 'require()'
        ["CoronaProvider.ads.admob"] =
        {
            -- required
            publisherId = "com.coronalabs",
        },
    },  

Now I just need to adjust some variables to correctly arange the ad at the screen.

Upvotes: 0

Krishna Raj Salim
Krishna Raj Salim

Reputation: 7390

In your build.settings check whether you've added the followings:

android =
  {
    usesPermissions =
      {
        "android.permission.INTERNET",
        "android.permission.ACCESS_NETWORK_STATE",
        "android.permission.READ_PHONE_STATE",
      },
  }

Keep coding.................. :)

Upvotes: 1

Related Questions