Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

B. Initialization and Listeners

Hadi Tavakoli edited this page Oct 11, 2018 · 4 revisions

In the previous step you found out how you should implement Admob into your project and how you should set the Air manifest .xml file. In this page you will see how you can initialize the Admob ANE and what are the general events, to listen to user interactions with the ads.

Initializing Admob

First, you should import the main Admob classes as follow:

import com.myflashlab.air.extensions.admob.*;

Second, you should initialize the Admob by calling the init method and pass in the flash.display.Stage instance and the Application ID you received in your Admob console in Step 2

AdMob.init(stage, "ca-app-pub-9002001127208746~3709582175");

/*
	If you want to initialize DoubleClick SDK instead of admob, all you have
	to do is to pass null as the second parameter when initializing the ANE:

	AdMob.init(stage, null);
*/

IMPORTANT: As an AIR programmer, especially if you are using FlashDevelop IDE for your developments, you know that the Stage reference will not have the correct dimensions if you call stageWidth and stageHeight too soon in your project. And this happens on the iOS side only! So, when you are building your App, make sure you are initializing the Admob ANE only when you are sure the Stage dimentions are final. If you don't notice this tip, you will spend many hours wondering why your Ads are not positioned correctly!

Third, you should listen to general ad events as follow. We're calling them general ad events because no matter what type of ad you are using, these events will occure. for example, when a user leaves your app because of touching on an ad.

AdMob.api.addEventListener(AdMobEvents.AD_CLOSED, 	onAdClosed);
AdMob.api.addEventListener(AdMobEvents.AD_FAILED, 	onAdFailed);
AdMob.api.addEventListener(AdMobEvents.AD_LEFT_APP, 	onAdLeftApp);
AdMob.api.addEventListener(AdMobEvents.AD_LOADED, 	onAdLoaded);
AdMob.api.addEventListener(AdMobEvents.AD_OPENED, 	onAdOpened);

private function onAdClosed(e:AdMobEvents):void
{
	trace("onAdClosed > " + e.adType); // AdMob.AD_TYPE_*
}

private function onAdFailed(e:AdMobEvents):void
{
	trace("onAdFailed > " + e.adType); // AdMob.AD_TYPE_*
	trace("onAdFailed > " + e.errorCode); // AdMob.ERROR_CODE_*
}

private function onAdLeftApp(e:AdMobEvents):void
{
	trace("onAdLeftApp > " + e.adType); // AdMob.AD_TYPE_*
}

private function onAdLoaded(e:AdMobEvents):void
{
	trace("onAdLoaded > " + e.adType); // AdMob.AD_TYPE_*
	
	if (e.adType == AdMob.AD_TYPE_BANNER)
	{
		
	}
	else if (e.adType == AdMob.AD_TYPE_INTERSTITIAL)
	{
		
	}
	else if (e.adType == AdMob.AD_TYPE_REWARDED_VIDEO)
	{
		
	}
}

private function onAdOpened(e:AdMobEvents):void
{
	trace("onAdOpened > " + e.adType); // AdMob.AD_TYPE_*
}