Admob in Flash with Adobe Air for Android
Have you read about StageWebView in Adobe Air SDK for flash? Basically, we can use it to display HTML content in Adobe Air Flash base application. But there is no interaction between actionscript and HTML content (no pass data or access properties). I’m not gonna to explain more detail on what is StageWebView. You can read more about it here: http://www.adobe.com/devnet/air/quick_start_as/quickstarts/qs_using_stage_web_view.html
I do some experiment to display admob content in Adobe Air Flash base Android application.This is how I do it.
Before that, make sure your mobile phone have a latest Adobe Air and latest Flash Player. Get it from Android market. And for your PC or laptop, make sure your machine installed with latest Adobe Flash Professional CS5 Extension for Adobe AIR 2.5.
1. Add site in Admob
For those has using admob before, we know they is several type of admob ad available. One of it is for “smartphone web”. It use javascript to display ad in website that optimize for smartphone view like iPhone and Android.
Fill in all detail. And then click “continue” button. Admob will provide a javascript code. Copy the code.
2. Create HTML file.
Paste the javascript you copy just now and add it in the HTML file. Upload the file to your domain. For testing, Use your mobile phone browser and go to the URL that you place the html file. You should see the admob test ad now. You can test my demo by point your mobile browser to http://m.dreamflashstudio.com/test/admob/
3. create flash file for android.
-Open Flash CS5. Create new flash file from template. From the template option, select “Air For Android”. And press OK button.
-Then create and add document class. Declare variable.
private var admobURL:String; private var webView:StageWebView;
- create StageWebView object
// create link to your html file that containt admob javascript that create previously admobURL = "http://yourdomain.com/yourfilename.html"; webView = new StageWebView(); webView.stage = this.stage; webView.viewPort = new Rectangle(0, 0, 510, 100); webView.loadURL(admobURL);
Now you are done on flash part. Compile it.
3. Test it – deploy to apk
When you publish fla file, flash will create XML file. You need to add a premission to access internet in the manifest part. Add this code between <manifestAdditions> tag.
<![CDATA[<manifest> <uses-permission android:name="android.permission.INTERNET"/> </manifest>]]>
Now you are ready to create a *.apk file. Install it in your phone. And run it. You will see the” admob test ad” in flash. (If you not sure how to create *apk file from SWF file, get the information here – Adobe Air for android.)
But wait, there is some problem right? When you click the ad, it will open the ad link in StageWebView!! We need to make sure the link is open in browser.
4. Add web change listener
Add a listener to stage view. Please read my comment in code.
// suppose to use LocationChangeEvent.LOCATION_CHANGING, but doesn't work for me (StageWebView still in beta maybe, or any suggestion?) // LocationChangeEvent.LOCATION_CHANGE - is call when changeURL // LocationChangeEvent.LOCATION_CHANGING - is call before changeURL webView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, adChange);
and after that add the function that will handle location change event
private function adChange(e:LocationChangeEvent):void
{
if (e.location!=admobURL) {
// use to prevent StageWebView load the new URL (it will work if pair with LocationChangeEvent.LOCATION_CHANGING)
e.preventDefault();
// since e.preventDefault() doesn't work for me, I set the webView to back to previous page.
webView.historyBack();
// open url - new window
var url:URLRequest = new URLRequest(e.location);
navigateToURL(url, "_blank");
}
}
5. The only problem I have
I dont know why LocationChangeEvent.LOCATION_CHANGING event on StageWebView has not dispatched. Anyone?
Ok that’s all for now. To make it easy for me to attach the Admob, I just create a class file that extend MovieClip. And link it to new MovieClip in Library. And I just need to attach by drag the movieclip to Stage, and set the url.
You can get the sourcefile here. I also have compiled *.apk file for quick install the admob demo and test it.
If you not sure how to create *apk file from SWF file, get the information here – Adobe Air for android.


cool articles worth to read..thanks for share bro..
tq sifuu… hait!!
thanks for the article.
I did everything by the book and I see the “Admob Test iPhone.js…” etc banner in my application.
But I don’t see other ads. And according to the Admob my account is inactive (red dot) since “Admob did never recieved an ad request from my site”. what does it mean? I did put the Html with the java script on my domain. Any ideas?
Cheers
Vivo
if I’m not mistaken, the “Test” version will not count as a request in your report. You need to disable the test attribute. Let it request the real one. Sometime your request didn’t show anything, but it consider as a “request”. And you will see the your red dot status become green dot. In the javascript, try to find this line [test: true], and change ‘true’ to ‘false’ Hope this help.
thanks,
I already changed that but now I get nothing (white square) and my account is still inactive. (the old red dot)
In the admob web site, when I add an application, what should I write under site URL? should I give my domain where my adMob.html is or the http://market.android.com/details etc?
I must do something stupidly wrong. Any ideas what?
Vivo
this my setting,
site url:http://m.dreamflashstudio.com/test/admob,
and it will work on any site under this path.
hey, I actually came up with a solution just like this, but before i turn the test mode off and put my app on the market, i was trying to go around the web and find other people that have done this and make sure they never got their admob accounts suspended. I asked admob here https://groups.google.com/forum/#!topic/google-admob-ads-sdk/85WFd2uOggQ
but havent gotten any replies. I read somewhere else someone saying that their admob account got suspended for click fraud. (maybe their situation was different because they were testing without test on or something) I’m wondering if how what we’re doing with the prevent default then opening it up in the native browser makes it look like a bad click. Have you been doing it an an application that gets a lot of traffic/clicks?
hi, I dont have any flash app run on android yet. For now I just play around with test mode. But if can solve my problem that I mention, it should be ok. But of course we need to ask admob guy to confirm our solution is valid or not.
will it work on AIR 2.5 for IOS?
I’m not sure about that
I can confirm you that it works very well on AIR 2.7/IOS
I have a question about the banner. I succeeded to implement an AdMob banner in my Android (Flash) App, whoohoo! And published the app in the market. If I install my own free app on the device I sometimes see the banner, otherwise its blank (white). I guess it isn’t normal. I also have the *.html within the app, because I don’t have a domain. Is that a problem? So I read the html file from the projects assets map:
Code snippet:
private var webView : StageWebView = new StageWebView();
private var workingFile : File = File.createTempFile();
//Set up web view object
webView.stage = this.stage;
webView.viewPort = new Rectangle(0, this.stage.stageHeight – 72, this.stage.stageWidth, 72);
webView.addEventListener(ErrorEvent.ERROR, onWebViewError );
webView.addEventListener(LocationChangeEvent.LOCATION_CHANGING, onWebViewLocChanging);
webView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, onWebViewLocChange);
//Copy the html file outside the app directory
var templateFile : File = File.applicationDirectory.resolvePath(“assets/adView.html”);
templateFile.copyTo( workingFile, true );
try {
webView.loadURL(workingFile.url);
} catch (e:Error) {
trace(e);
}
Can someone explain me why the banner is mostly blank?
Thank you very much.
Hi Philipp, Admob not always showing the ad. Sometime is showing and sometime is not. That’s normal. If there is no suitable ad for your target audience/user, it will not showing any ad. Target audience have many factor, user age maybe, or location/country, language. For example Admob have ad related SALE IN NEW YORK, but your user is in Australia, so this ad will not show up to this user.
After a lot of trouble (account canned on ADMOB ) and research, I have gotten Ads to work in all my Android Apps. This will work on a lot of AD networks, but most will ban you click fraud. Only one network allows this method and they provide support for it too.
I have over 100 games apps. with this method implemented and working. Here is a link to one of them for you to see how it will look in game. I am using multiple ads in this to force the user to click and make me some money: https://market.android.com/details?id=air.GraffitiCityMarketFree&feature=search_result
Does LeadBolt offer HTML integration for banner ads?
LeadBolt does allow banner ads to be integrated into your app using HTML, rather than using our SDK. To create a HTML banner ad after adding an app to the LeadBolt portal, simply click “Add Ad” and select “App Banner (HTML)” from the drop down box. The HTML snippet can then be added directly into your app’s HTML framework.
So far my eCPM is $6.15
I have created this guide to show my appreciation:
Publisher Code:
STEP I:
Get an Account: http://leadboltapps.com/web/publishers/signup.php?ref=10022842
STEP II:
Click on the “APPS” tab and “Create New APP” to create an AD. Remember to change content unlocker to HTML Banner. While in the process.
STEP III:
Get the HTML AD Code and keep it safe. That is all we need from the site. How simple was that?
AD HTML FILE:
Create an HTML File and Load it to your site. Remember to replace your HTML Code from above step with where I have put: ****ENTER HTML AD CODE HERE****
Untitled Document
body,td,th {
color: #FFF;
}
body {
background-color: #000;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
text-align: center;
position: relative;
}
****ENTER HTML AD CODE HERE****
Action Script Code:
STEP I:
Credit: I found this on another site and would like to give credit to the author of pixelpaton.com
The only change you need to make is to enter your website html url where you have placed the AD HTML FILE in the space where I have put : “****ENTER COMPLETE HTML URL HERE****”. Where ever you want the AD, place the following code:
// imports
import flash.events.Event;
import flash.events.LocationChangeEvent;
import flash.geom.Rectangle;
import flash.media.StageWebView;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.events.MouseEvent;
// setup variables
var _stageWebView:StageWebView;
var myAdvertURL:String = “****ENTER COMPLETE HTML URL HERE****”;
//
{
// check that _stageWebView doersn’t exist
if (! _stageWebView) {
_stageWebView = new StageWebView () ;
// set the size of the html ‘window’
_stageWebView.viewPort = new Rectangle(0,0, 800, 100);
// add a listener for when the content of the StageWebView changes
_stageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGE,onLocationChange);
// start loading the URL;
_stageWebView.loadURL(myAdvertURL);
}
// show the ad by setting it’s stage property;
_stageWebView.stage = stage;
}
function toggleAd(event:MouseEvent):void {
trace(“toggling advert”,_stageWebView);
// check that StageWebView instance exists
if (_stageWebView) {
trace(“_stageWebView.stage:”+_stageWebView.stage);
if (_stageWebView.stage == null) {
//show the ad by setting the stage parameter
_stageWebView.stage = stage;
} else {
// hide the ad by nulling the stage parameter
_stageWebView.stage = null;
}
} else {
// ad StageWebView doesn’t exist – show create it
}
}
function destroyAd(event:MouseEvent):void {
// check that the instace of StageWebView exists
if (_stageWebView) {
trace(“removing advert”);
// destroys the ad
_stageWebView.stage = null;
_stageWebView = null;
}
}
function onLocationChange(event:LocationChangeEvent):void {
// check that it’s not our ad URL loading
if (_stageWebView.location != myAdvertURL) {
// destroy the ad as the user has kindly clicked on my ad
destroyAd(null);
// Launch a normal browser window with the captured URL;
navigateToURL( new URLRequest( event.location ) );
}
}
// setup button listeners
Hope this works and helps you. If you have questions, let me know. Enjoy.
Man im trying to use html ads of lead bolt but it doesnt work!
i just got and black screen! its strange the implementation in the html its just place the snippet with js? the js seems doest exist! look >> http://ad.leadboltads.net/show_app_ad.js
and this is the intire code
it should work when we test the url in a brower, or in a mobile brower, or just works in the compiled app? even the app doesnt works for me! heeelllpp!
[...] that the StageWebView was not working properly. I did some research and found this web site: blog.dreamflashstudio.com. After reading thought it I added the following code to my [...]