Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [79]

By Root 2509 0
format

var myString:String =

""

+ ""

+ "Hello
"

+ "Look at me

"

+ "Click Me"

+ ""

+ ""

webView.loadString(myString, "text/html");

webView.addEventListener(LocationChangeEvent.LOCATION_CHANGING, onChanging);

// load the Google site into the viewport when clicking on the text

function onChanging(event:LocationChangeEvent):void {

event.preventDefault();

webView.loadURL(event.location);

}

At the time of this writing, this feature is limited and does not provide much more than a TextField with htmlText would provide. You cannot, for instance, use it to display local assets such as images. It is useful if you want text placed in the StageWebView area.

Mobile Ads


Local use in combination with JavaScript is handy when dealing with mobile ads.

At the time of this writing, mobile advertising providers do not offer an AS3 SDK. Rewriting their AS2 code into AS3 to use in AIR does not seem to get ad impressions. Using StageWebView to simulate the browser model seems to be the best solution.

Go to http://developer.admob.com/wiki/Android or http://developer.admob.com/wiki/Requests for instructions on how to set up an ad request for AdMob before we adapt them for AIR for Android.

You need an embedded HTML page that makes the request to the ad provider with your publisher ID and the StageWebView to load the page and monitor the navigation. The HTML page contains a JavaScript script that fetches the ad. It is important to set manual_mode to true. Set test to true during development or to false to receive live ads. The parameter in the getElementById method must match the name of the div in the page. Here we call it ad_space. See Figure 13-1 for an example of how the ad is displayed:

Get Ad

Figure 13-1. AdMob ads with the test property set to true (top) and false (bottom)

The ActionScript looks like this. Put your ad request in a try catch block in case the ad provider site is down or it sends back invalid data:

import flash.media.StageWebView;

import flash.geom.Rectangle;

import flash.events.LocationChangeEvent;

import flash.events.ErrorEvent;

import flash.filesystem.File;

import flash.filesystem.FileMode;

import flash.filesystem.FileStream;

import flash.media.StageWebView;

import flash.net.navigateToURL;

import flash.net.URLRequest;

var view:StageWebView;

var local:File;

view = new StageWebView();

view.stage = this.stage;

view.addEventListener(LocationChangeEvent.LOCATION_CHANGE, onChange);

view.addEventListener(LocationChangeEvent.LOCATION_CHANGING, onChanging);

view.addEventListener(ErrorEvent.ERROR, onViewError);

view.viewPort = new Rectangle(0, 0, 480, 60);

var base:File = File.applicationDirectory.resolvePath("adView.html");

local = File.createTempFile();

base.copy(local, true);

try {

view.loadURL(local.url);

} catch(error:Error) {

}

function onChanging(event:LocationChangeEvent):void {

event.preventDefault();

navigateToURL(new URLRequest(event.location));

}

// when the user clicks on the ad

function onChange(event:LocationChangeEvent):void {

if (event.location != local.url) {

navigateToURL(new URLRequest(event.location));

try {

view.loadURL(local.url);

} catch(error:Error) {

}

}

}

function onViewError(error:ErrorEvent):void {

trace(error);

}

Services and Authentication


Many services, such as Twitter and Facebook, require an authentication token that can only be obtained via an HTML web page. You can use StageWebView to call authentication

Return Main Page Previous Page Next Page

®Online Book Reader