【问题标题】:Admob in Corona is not running at the phoneCorona 中的 Admob 没有在电话上运行
【发布时间】:2014-04-20 11:50:56
【问题描述】:

我使用 Corona 创建了一个示例应用,并尝试实现 admob 功能。

我运行了 admob 示例,它工作正常。但是当我将它与下面的示例代码结合使用时,它就停止了工作。

在模拟器上它运行正常。但是当我将apk上传到我的手机时,我得到了一个黑屏。

不是错误,而是奇怪的黑屏。

你知道我做错了什么吗?

-----------------------------------------------------------------------------------------
--
-- 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

【问题讨论】:

    标签: android admob apk coronasdk


    【解决方案1】:

    在您的build.settings 中检查您是否添加了以下内容:

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

    继续编码...... :)

    【讨论】:

    • 谢谢krs。但仍然无法正常工作。我想知道场景和广告请求之间是否存在一些不兼容......我是否可能有一个特定的地方可以放置一些功能......
    【解决方案2】:

    好吧,只需添加这个,它就起作用了:

    -- ************* 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
    

    并且,在 build.settings 文件中:

    添加这个:

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

    现在我只需要调整一些变量以正确排列屏幕上的广告。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 1970-01-01
      • 2014-04-10
      相关资源
      最近更新 更多