## Tuesday, March 5, 2013

### Creating Ads Manger for Android - Part 5 - Implementing Leadbolt

previous parts

Finally, we are making ad manager to handle multiple networks, so it is not wise to rely only on one network.
If you intent to use Leadbolt you can register through this link. If you do so, I will get some small reward from them :-)

This should be no problem. Leadbolt has very good and simple manual. It is located under App Developer SDKs in HELP/FAQS section. Actually it is the same as AdMob integration - copy .jar with SDK into libs folder and then adjust Build Path in Project Properties (right click on project name in Eclipse). Then Add JARs...

As for previous ad networks we have to adjust manifest file with permissions. Leadbolt has three required permissions: INTERNET, ACCESS_NETWORK_STATE and READ_PHONE_STATE. The first two we already have from times of AdMob implementation. There are three optional permissions: ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION and ACCESS_ LOCATION_EXTRA_COMMANDS. The coarse location we already allowed for Madvertise so we adjust the manifest like this:

  <!-- additional LeadBolt permissions -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>


While most of the other networks has some way how to set refresh rate for Leadbolt you have to do it by yourself. So in the file with constants (SBCAdsConstatnts.java) we will define these lines:

 // Leadbolt
public static final long LEADBOLT_REFRESH_RATE = 1000 * 90 * 1;

The first line contains the ID your ad unit is assigned by Leadbolt. The second line defines refresh rate in millis - here 1,5 minute.

Now let's start implementation. As usual the imports and fields come first:

package com.sbcgames.sbcads;import com.pad.android.iappad.AdController;import com.pad.android.listener.AdListener;import com.sbcgames.sbcengine.SBCEngine;import android.util.Log;import android.view.KeyEvent;import android.view.ViewGroup;public class LeadboltSystem implements SBCAdSystem, AdListener
{
private final String SYSTEM_NAME = "LEADBOLT ";
private final ViewGroup mLayout;
private Runnable mR = null;
private SBCEngine mSBCEngine = null;

The constructor is very simple:

 // ------------------------------------------------------------------------
{
mSBCEngine = aSBCEngine;
mLayout = aLayout;
}

Next follows implementation of our SBCAdSystem interface:

 //------------------------------------------------------------------------
//------------------------------------------------------------------------
@Override
public void start()
{

mR = new Runnable()
{
public void run()
{

{
stop();
destroy();
}

}
};

mLayout.postDelayed(mR, 0);
}

Here is problem that I encountered: to refresh ad, calling to destroyAd() (called in destroy() method) and then calling to loadAd() should be enough. But the refreshing in this way did not work for me. So I am destroying the mAdView (the AdController object) every time and creating new one. I posted question to forum and I got reply from user called jay-z. While the above method works, I will try his code and update this article accordingly if it will work for me too.

Here is implementation of next methods - nothing special here:

 //------------------------------------------------------------------------
@Override
public void stop()
{
if (mR != null)
{
mLayout.removeCallbacks(mR);
}
}

//------------------------------------------------------------------------
@Override
public void destroy()
{
{
}
}

//------------------------------------------------------------------------
@Override
{
mListener = aListener;
}

In last two parts when implementing AdMob and Madvertise we left our keyDown implementation empty. For Leadbolt we have to handle back key. This is the reason why we have this method in interface. Of course, to avoid implementation in every ad system we could create abstract class between interface and concrete implementation (that will do nothing) and override it only when we need specialised behavior - I will leave this small change on your choice:

 //------------------------------------------------------------------------
@Override
public boolean keyDown(final int aKeyCode, final KeyEvent aEvent)
{
if (aKeyCode == KeyEvent.KEYCODE_BACK)
{
{
Log.d("AdsSystem", SYSTEM_NAME + "back key pressed");
return true;
}
}

return false;
}

 //------------------------------------------------------------------------
//------------------------------------------------------------------------
@Override
{
stop();
mSBCEngine.sendMessage(SBCEngine.MESSAGE_OUT_PAUSE, 0, 0);
}

//------------------------------------------------------------------------
@Override
{
}

//------------------------------------------------------------------------
@Override
{
}

//------------------------------------------------------------------------
@Override
{
}

//------------------------------------------------------------------------
@Override
{
mListener.onFailed(this);
}

//------------------------------------------------------------------------
@Override
{
}

//------------------------------------------------------------------------
@Override
{
}

//------------------------------------------------------------------------
@Override
{
}

//------------------------------------------------------------------------
@Override
{
}}