Skip to main content
Participating Frequently
March 13, 2013
Question

HTML Ad in Air for Android app using Flash CS6 (launching the native browser for banner clicks)

  • March 13, 2013
  • 4 replies
  • 8809 views

Hi guys, I am having difficulties integrating LeadBold HTML banner ads into my Air for Android app.

I'm having problems with launching the native browser when the banner is clicked. The link opens inside the stagewebview instance. I am using the sample provided by LeadBolt. I have followed the instructions set in the read me file. My app is still in TEST MODE. I have uploaded the html to my server, and changed all the necessary data. Also  when setting up the ad, open in new window is selected.

After dozens of testing with the code, I came to the conclusion that the  code below:

webView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, loadAds);

is only fired once, and does not fire again when the ad is clicked, hence it loads the link inside the webview.

When I remove the: if(e.location != admobURL) statement, it loads the URL in a new window automatically as soon as the banner is displayed without a LocationChangeEvent.

Is it because the adMob URL is always the same in test mode hence if(e.location != admobURL) is not executed? It does not execute the code inside the above if statement or LocationChangeEvent is not fired when the banner is clicked , hence navigateToURL is not executed. I may be wrong but any solution will welcome. I am using Adobe Air for Android version 3.2

package  {

import flash.display.MovieClip;
import flash.geom.Rectangle;
import flash.media.StageWebView;
import flash.net.URLRequest;
import flash.events.LocationChangeEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.events.MouseEvent;

public class LoadAds extends MovieClip {
private var admobURL:String;
private var webView:StageWebView;

public function LoadAds() {
// constructor code
//please change following string to your own url link, like
//"http://10.1.2.1/xxx/leadbolt.html"
admobURL = "My URL is here"; 

webView = new StageWebView();
webView.stage = this.stage;
webView.viewPort = new Rectangle(0, 0, 480, 72);
webView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, loadAds);
webView.loadURL(admobURL);               
}

function loadAds(e:LocationChangeEvent):void {
if(e.location != admobURL){
e.preventDefault();
webView.historyBack();
var url:URLRequest = new URLRequest(e.location);
navigateToURL(url);
}

}
}     
}

This topic has been closed for replies.

4 replies

Participant
June 8, 2014

package  {
import flash.display.MovieClip;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.media.StageWebView;
import flash.net.URLRequest;
import flash.events.LocationChangeEvent;
import flash.net.navigateToURL;

public class LoadAds extends MovieClip {
  private var admobURL1:String;
  private var webView1:StageWebView;

 
  public function LoadAds() {
   admobURL1 = "http://www.example.co.uk/examplebanner.html";
   webView1 = new StageWebView();
   webView1.viewPort = new Rectangle( 0, stage.stageHeight-100,stage.stageWidth , stage.stageHeight );
   webView1.addEventListener(LocationChangeEvent.LOCATION_CHANGE, loadAds);
   webView1.loadURL(admobURL1);


 
 
  function loadAds(e:LocationChangeEvent):void {
   if(e.location != admobURL1){
    e.preventDefault();
    webView1.historyBack();
    var url:URLRequest = new URLRequest(e.location);
    navigateToURL(url); } }

}

}

}

This Action Script code will execute a Leadbolt html banner at the bottom of the screen. You also need to create a html page with the Java Script banner link provided by Leadbolt.

Example: Word Funk FREE - Android Apps on Google Play

Participant
February 7, 2014

Hi,

I had the same problem, but with MJD1981's id the event fired successfully (btw thanks for sharing your code !). So I think this is due to LeadBolt platform, not AIR.

Then I tweaked some parameters of my HTML banner in my own LeadBolt console, set ad format from 'Text and banner Ads' to 'Text Ads only' and then, guess what, the locationChangeEvent.LocationChange fired successfully... I set back this parameter to 'Text and banner Ads' again and the event still fire (so far).

As a result it works perfectly (open Chrome with the expected URL on my Nexus 7 2013, AIR 4.0.0.1390, Android 4.3)

HTH

Known Participant
January 26, 2014
MJD1981
Inspiring
March 14, 2013

I recall I had to keep "LocationChangeEvent" in check with a couple of variables of my own (basically I check whether it has fully loaded or not before I look for clicks). Here is my personalized code, which I have been successfully using for the past 6 months:

//CREATE AD

function ShowAd(startX:int,startY:int,bannerWidth:int,bannerHeight:int):void {

  adWindow = new StageWebView();

  adWindow.stage = this.stage;

  adWindow.viewPort = new Rectangle(startX,startY,bannerWidth,bannerHeight);

  adURL= *YOUR URL*

  adWindow.loadURL(adURL);

  adLoaded=0;

  adWindow.addEventListener(Event.COMPLETE,LoadedAd);

  adWindow.addEventListener(LocationChangeEvent.LOCATION_CHANGING,UpdateAd);

  adShowing=1, adTim=0;

  adLinked=0;

}

//successfully loaded

function LoadedAd(event:Event):void{

  adLoaded=1;

}

//acknowledge interaction

function UpdateAd(event:LocationChangeEvent):void {

  trace("Location changing...");

  if (adLoaded>0 && event.location!=adURL && adLinked==0) {

    trace("Navigating to "+event.location);

    navigateToURL( new URLRequest(event.location) );

    adClicked=1, adLinked=1;

    RemoveAd();

  }

}

//remove ad

function RemoveAd():void {

          trace("Removing ad");

          adWindow.removeEventListener(Event.COMPLETE,LoadedAd);

     adWindow.removeEventListener(LocationChangeEvent.LOCATION_CHANGING,UpdateAd);

          adWindow.dispose();

          adShowing=0;

}

You can see my ads implemented on Android here:
https://play.google.com/store/apps/details?id=air.WRCareer

And on iOS here:

http://itunes.apple.com/app/wrestling-revolution-free/id542665773

MJD1981
Inspiring
March 14, 2013

If you find this information useful and are interested in implementing HTML ads, I'd appreciate it if you used my referral link:
http://www.leadbolt.com/developers_signup.php?ref=10039988

msesenAuthor
Participating Frequently
March 14, 2013

Hi MJD1981, thanks for the reply. I am at work now will try your solution when I get home.

Last night I've changed the URL to something like bbc.co.uk just to see if locationchangeevent fired when a link was clicked inside webView and vollaaaaa the bbc.co.uk was loaded and when i clicked a link it was opened in nativebrowser.

Maybe it's something to do with the javascript inside the leadbolt.html?

Will try your solution, and will refer you. If this doesn't work im going to give up, have tried for the last 4 days and will never use adobe air or flash to develop mobile apps, as there is no documentation/support for critical features such as monetizing an app. C'mon Adobe.

Break finished, back to work. Damned can't wait to get home to try the solution :)