- Creating Ads Manger for Android - Part 1 - First part of SBCAdManager
- Creating Ads Manger for Android - Part 2 - Finishing SBCAdManager
In Part 2 we finished our SBCAdManager class. Now we have system that will choose the right ad network for us as well as check whether the network is alive (sending ads) and change to another one if the network is dead. But so far we did not implement any ad network. As was said in previous parts we will step by step implement AdMob, Madvertise and Leadbolt. The system we are developing in this series is working in our real games Deadly Abyss 2 and Mahjong Tris.
This time we will implement first of the ad networks - AdMob. (for clarity, I am using terms ad network and ad system interchangeably)
To provide common set of methods to control each of the implemented ad systems we will introduce interface class SBCAdSystem. In each of the ad systems in our ad engine we will implement the methods. Remember Part 1 and picture with all the files in com.sbcgames.sbcads package. The interface is in SBCAdSystem.java file:
Adding AdMob SDKGo to Google Developers site to get the latest version of SDK. Or you may have it already as a part of downloaded SDK in "extras" directory (mine path is: D:\Android\android-sdk\extras\google\admob_ads_sdk\). To incorporate it into your project follow steps 1-3 at Google Developers (do not forget to choose "Android" in the top of the page or you will see iOS related how-to).
Adjusting manifestEvery Ad network needs some permissions enabled. AdMob is not as hungry as other so internet access and network state reading is enough. Put the following lines into your manifest file:
You also need to define AdMob activity inside your application tag:
Adjusting constantsNext we will adjust our file with constants (file SBCAdsConstatns.java) . I am again referring to Part 1 where the file is listed. Bellow is only the line related to AdMob implementation. Replace the text of the constant with the ID you get from AdMob site after you register your app - click Manage Settings and you should see something like this:
AdMobSystem.javaNow you are ready to real implementation. Start with imports and variables:
The class implements SBCAdSystem interface as well as AdListener interface. The AdListener interface is part of AdMob package and its implementation will work as callback for various events like when the ad is received or when the request for ad failed. Our interface is here to unify the different ad systems and to provide some common set of methods to start, stop and so on the system.
The constructor looks like this:
We store the reference to aLayout which is "adsholder" element in our main.xml layout file. This file we created in first part. Next we create new ad view - this comes from AdMob SDK. We add listener to it - it is the AdListener interface implementation and finally we add this new view into prepared "adsholder" in layout.
Now the object is created but we have to ask for ad. This is done in start method:
We first set view to be visible. Then we create request for AdMob ad. In following lines we can add test devices. I highly recommend this as this will deliver test ads into your device so clicking on them will not violate ad company policy. You can add the emulator as well as real devices you are testing your apps on.
Finally we launch our request. You will be notified if it is or is not successful through AdListener interface implementation.
SBCAdSystem implementationThe following 5 methods are implementation of SBCAdSystem interface.
As we want to change from one ad network to another, we need a way how to stop and destroy the ad view:
These two methods are the ones called from SBCAdManager from stopAndDestroyAdSystem() method. It will disable displaying the view and cancel pending requests for ads. And in destroy() method it will destroy the ad view and remove it from "adsholder", so it is empty again and ready to hold any other ad view.
Last two methods from SBCAdSystem Interface are setEventListener() and keyDown(). The later has no implementation for AdMob, so we just say that AdMob did not consumed the sent key. The first one sets listener for events of this system. The listener is SBCAdManager class itself - it will listen for events from each ad system and based on it it will terminate network, change to another and so on: