Got Admob working for Android :)

hi kam187,thanks for your reply.I have added the permissions on android,and i have had a admob publisher ID.The status now is active.when test is true in ad.html,it work fine,there is a test ad show up.but when i change the test is false in ad.html,nothing display.Look forward to your reply.Thanks. [import]uid: 47530 topic_id: 8021 reply_id: 41818[/import]

@kam187

I wonder if there’s a way to store the publisher id in the lua code instead of the ad.html file?

Since there are users with Jailbreaked devices that can change the Publisher Id with their own and steal our revenue, I’ve seen that on many forums that a lot of people does that to make a few extra $.

[import]uid: 34126 topic_id: 8021 reply_id: 41855[/import]

benvsweb - Do you ever call DisaplyAd(0) ? You need to call that to show the ad. The webpopup is ALWASY on top, so just as a test call DisplayAd(0) at the begining of your main.lua. The ad should show at the bottom.

zyg@accessport.cn - do you run any ad blocker on your phone? Check a game like angry birds and see if you see any ads in it. Also go to whatsmyua.com in your browser on your phone and verify it says ‘Android 2.x.y’ (where x and y can be anything)

holmes2870 - Yes it would be possible to embed the id inside the lua code but after thinking about it, I came to the conclusion there’s very little point - the idea of editing the id makes no sense to me at all:

There’s very little advantage of someone editing your id out of your app on their own device. What would be the point? The ads displayed would be linked to their account - sure, but what would they really gain?

If they wanted to display ads for their account on their device and get impressions they could just run their own app :confused: If they want to click their own ads to gain revenue they could do that in their own game too. Doing either of these wouldn’t work for very long anyway since all the ads would show the same IP address and admob would probably suspend the account pretty quick.

At first sight what you’re saying sounds really bad, but think it through a little and its totally pointless for anyone to do that. If you’re saying people edit out your id and release a pirate copy of your game - 99.999% of games with ads are free. Why would anyone want to download a copy of a free game from an untrusted source when they can get it direct from the App Store or Android market?

In any case, if for some crazy reason someone did want to edit you’re id, the lua code itself is not hard to edit, even once the game is compiled.

I’d be interested to see where you read this and what discussion it led to? It sounds more like FUD to me. [import]uid: 8872 topic_id: 8021 reply_id: 41891[/import]

kam187:
i used the main lua code you posted along with the .html codes
you supplied. But I see no white rectangle at all in the corona simulator?

do I need to add anything other than the code you have in the
first post?

thanks [import]uid: 67514 topic_id: 8021 reply_id: 41927[/import]

yes, when you want the ads to show u need to call:

DisaplyAd(0)

And to remove them

RemoveAd(0)

For example. You may not want ads on your menu screen. But when the game starts u want ads. So call DisplayAd(0) at that point.
Maybe you want the ads to start in 1 second so then use DisplayAd(1000).

Then when the game ends and you go back to the menu you might want to call RemoveAds(0) to remove them again :slight_smile: [import]uid: 8872 topic_id: 8021 reply_id: 41928[/import]

Thank you Kam works perfectly :slight_smile: [import]uid: 67514 topic_id: 8021 reply_id: 41949[/import]

Any ideas why it wouldn’t work on Android 3.0? CoronaSDK webpopup issue? [import]uid: 4596 topic_id: 8021 reply_id: 41977[/import]

Its not the webpopup. I have it working on 3.0 but I don’t think its wise for _everyone_ to use this method. If you have a tablet centric app and realllllly need it you can find me on irc for the solution :stuck_out_tongue: [import]uid: 8872 topic_id: 8021 reply_id: 41978[/import]

@kam187

Nice! Why do you think this? [import]uid: 4596 topic_id: 8021 reply_id: 41979[/import]

Rather not say here :slight_smile: [import]uid: 8872 topic_id: 8021 reply_id: 41980[/import]

Dear Kam,

As usual you are great!

I had to add this extra line:

local isAndroid = “Android” == system.getInfo(“platformName”);

just before the function DisplayAd(t)

to make it work.

I wrote a simple physics-based game (shooting aliens with particles) and included your code. This is on Windows and Android. Tested it on my Galaxy Tab. The ad displayed fine and when I touched on it, the webpopup shows full screen (the Admob Test Page). Then, when I touched on the return button on my galaxy tab, I expected it to return to the game, but instead, it shows a black screen. After 5 seconds, it Force Closes with the usual Android Force Close message (The app … has stopped unexpectedly. Please try again.). Since this happens on the actual device, I am not able to debug it. The Corona Simulator does not support Web Pop Ups. I am at a loss on how to debug this issue.

Any advice would be much appreciated :slight_smile:

And thanks again for your willingness to share your knowledge!
[import]uid: 64189 topic_id: 8021 reply_id: 42261[/import]

Oops, I have that global in my app to use different implementations in my game :stuck_out_tongue: I’ve added the line to the code in post 1 now.

I’ll give you a more detailed response to your question so you can understand whats going on…

When you switch to another app (eg. when you click the ad, or press the home button) Android sends your app a suspend message. This tells the app it should sleep.

There are two kinds of suspend:

  1. Sleep - program remains in memory but paused.
  • You press home, or switch to another app

  • Android sends a ‘suspend’ event

  • Corona process is frozen, but the game remains in memory.

  • You return to your game

  • Android sends a ‘resume’ event

  • Corona process is unfrozen and the game continues from where it left of

Issues: Corona itself actually freezes your game, and resumes it. In some versions of corona this is broken. I use 319 (for reasons in my tutorial page), and although it works fine, it very rarely has a flickering problem with the screen - but that sorts itself out in about 3 or 4 seconds.

The latest daily has an issue with resuming from suspend at the moment

  1. Suspend - program it removed from memory.
  • You let the screen go to sleep, and wait 5 to 10 seconds

  • The phone sends you a ‘suspend’ event [You should save state]

  • The phone send you a ‘exit’ event [you should exit immediately]

  • The phone FORCES you’re app to quit if it hasn’t already

  • You wake up you’re phone

  • Your app will be launched with a ‘resume’ event being passed immediately.

Unless you saved your state your game will just launch from the beginning.

Issues: If you have some save file or other form of save state inside your game, your app may be forced to quit mid game. If you’re not prepared for this, this score file could end up in a inconsistent state.

You are responsible for saving your state to disk, and reloading it on resume. Corona won’t do this for you.

Most devices send the following sequence on waking up from this kind of suspend:

‘resume’
‘exit’
‘suspend’
‘exit’
‘resume’

God knows why - but this could cause problems in your game too.


Tracking the issue:

You can use the system runtime event:

  
local onSystemEvent = function( event )  
 if event.type == "applicationSuspend" then  
  
 elseif event.type == "applicationExit" then  
  
 elseif event.type == "applicationResume" then  
  
 elseif event.type == "applicationStart" then  
  
 end  
end  
  
Runtime:addEventListener( "system", onSystemEvent )  

Note: Application Start is if you’re app is started normally, and not returning from a resume that had previously exited your app.

You can place some print statements in there, and use adb logcat or eclipse’s debugger to capture the log output on your device.

Most likely you will have to either place os.exit() inside the suspend condition to cleanly exit and forget about suspend/resume OR you’ll have to implement a save state and do it inside the ‘suspend’ event and quit. Then on resume reload that file.

Or if you just want to support sleep and not suspend. Switch to a different corona version. I highly recommend 319 unless you really need 2.2 only and fast devices. In which case try a version <=525

hope that helps. [import]uid: 8872 topic_id: 8021 reply_id: 42266[/import]

Dear Kam,

Thanks again for your valuable advice and tips.
I have modified your code and use it as follows:

local onSystemEvent = function( event )  
 if event.type == "applicationSuspend" then  
 if (system.getInfo( "environment" ) ~= "simulator")then   
 os.exit();   
 end  
  
 end  
end  
  
Runtime:addEventListener( "system", onSystemEvent )  

This is to enable Corona Simulator to Build the app. It goes into suspend mode when building. Now, when the user touches the ad banner, the suspend function is called and the app/game is killed. This way, when the user touches the back button of the Galaxy Tab it won’t cause the FC which I mentioned earlier.
And yes, you are right, Corona does not handle the suspend and resume properly (for Android at least) - it causes the app/game to crash when returning from a web pop up. I’m currently using Build 505. (Build 544 is even worse, any app built with 544 crashes on Galaxy Tab - I have reported the bug case #6487). I tested with the Sample Hello World code and it also crashes when returning from a web popup - this indicates it is a bug with Corona SDK. I wish the Ansca team will fix it because it would be bad for the User to find that the app/game has closed when returning from a web popup. Meanwhile, I will have be content with just closing the app/game when the web pop up is shown.
Official AdMob (and iAds) support from Ansca would be sweet. But alas, I have read about the lawyer-delay issue.

I also modified the main.lua portion as follows:

local isAndroid = "Android" == system.getInfo("platformName");  
function DisplayAd(t)  
 native.cancelWebPopup()  
 timer.performWithDelay(t, function()  
 local adfile = "apple\_ad.html"  
 local sizeX = 320   
 local sizeY = 48  
 --local scale = 1/display.contentScaleY  
   
 if isAndroid then  
 adfile = "android\_ad.html" --OK for Droid and Galaxy Tab  
  
 adSpace = native.showWebPopup((display.contentWidth - sizeX)/2, display.contentHeight - display.screenOriginY - sizeY, sizeX, sizeY, adfile, {baseUrl = system.ResourceDirectory, hasBackground = false, urlRequest = showAd\_Android})  
 elseif system.getInfo( "environment" ) == "simulator" then  
 adSpace = display.newRect( (display.contentWidth - sizeX)/2, display.contentHeight - display.screenOriginY - sizeY, sizeX, sizeY )  
 end  
 display.getCurrentStage():insert(adSpace, false)  
 end  
 )  
end  

Note that I am only building for Android (not iOS). The 1.5 scaling does not work properly for Galaxy Tab and Droid. The ad banner is chopped off on the right. Disabling scaling and using solely the android_ad.html version, solves the issues.

And lastly, thanks a million again for your help and guidance! [import]uid: 64189 topic_id: 8021 reply_id: 42379[/import]

Hmm thats odd about the scaling. Maybe you need to add 2 px for the android version. Are you using portrait or landscape?

I tested the scaling in the android emulator. The issue without it, is that the webpopup will scale with the normal autoscaling in corona, but webkit will only zoom a max of 1.5 by default. Therefore no matter how big your device screen is, the max ad size will be a scale of 1.5.

Without the scaling, the webpopup becomes larger than the ad. The ad loads to the left of the popup and then jumps to the center.

When you say chopped off, do you mean you see scroll bars in the webpopup? How much is chopped off? Can you take a screenshot maybe?

I better fix it in my app if thats the case :slight_smile: [import]uid: 8872 topic_id: 8021 reply_id: 42382[/import]

Hi Kam,
Thank you for the solution! It works nicely on both platforms.

Dusan
http://addictivezone.com

[import]uid: 5411 topic_id: 8021 reply_id: 42414[/import]

Dear Kam,

You can see the ad banner screenshot here:

http://paulmcpd.blogspot.com/2011/06/admob-screenshots.html
I also posed a question about which AdMob Site/App Ad to choose when adding new Admob App/Site? (See the screen shot - also in the above blog, should I choose A, B, or C). Hope you can advice. Thanks :slight_smile:

Another question (sorry for so many questions ). In the android_ad.html file, there is an option called ama.
Does this refer to AdSense ads? Google has integrated AdSense and AdMob and they are displaying AdSense ads when there is no fill for AdMob. Should I enable ama = true to get AdSense ads.
I could test it out for my next game.

Once again, thanks for reading and taking the trouble to reply and sharing your knowledge :slight_smile:

Dear dpavlica/Dusan,

I installed your DeviceInfo app on Galaxy Tab.
When run, I get a black screen. Used back button - no response. Use Home button ok. Which Corona built are you using?

I had the same issue with Built 544, and so, now, I use only Built 505.
[import]uid: 64189 topic_id: 8021 reply_id: 42437[/import]

Hi droidsearch,
I have the same issue with FC on my Nexus One (319 build).
I will use the applicationSuspend to solve it temporarily.

BTW: Have you experienced the following issues on your Galaxy Tab?:
http://developer.anscamobile.com/forum/2011/06/22/shifted-screen-galaxy-tab
http://developer.anscamobile.com/forum/2011/06/10/android-touch-problems-galaxy-tab#comment-42316

Please let me know…

Thanks,
Dusan
[import]uid: 5411 topic_id: 8021 reply_id: 42417[/import]

Hmm thats strange. I guess the tab doesn’t abide by the viewport settings.

With regards to the admob option, you should be using A. Ignore the sdk download. You should also set the refresh rate to 20 seconds. The html file does a refresh every 30 seconds, so this makes sure a new ad is ready on admob’s side.

Ama isn’t for adsense. Its for a special type of ad used on websites. There should be an option in admob to enable adsense. You need to qualify to have the option enabled. I’m not entirely sure how, but I have it in my account. Other people I know dont :confused: [import]uid: 8872 topic_id: 8021 reply_id: 42471[/import]

Kam187 recommended using build 319. The oldest build that I can see on http://developer.anscamobile.com/downloads/daily-builds is build 521.

Is build 319 stored somewhere else on Ansca web site? [import]uid: 65052 topic_id: 8021 reply_id: 42986[/import]

The link is in section 7 of my tips page:

http://www.base2solutions.com/walkabout/Corona%20Tips.html [import]uid: 8872 topic_id: 8021 reply_id: 43114[/import]