previous parts
Last
time we implemented first ad network - AdMob. Today we will add Madvertise. This network is exactly the reason why we are building some ad systems manager. It seems that Madvertise is focused on some countries of western Europe - Germany, Italy. Earnings are about 0,05 EUR / click, which is not bad. But if your user is in any other location they will not send an ad into his device. This results into low fill rate but if you have ad manager you can just say never mind and skip to other ad network.
Adding Madvertise SDK
To integrate Madvertise SDK into your Eclipse project follow the instructions
here. It is enough to follow the instructions until you added Madvertise project into your project as rest may be little bit different for out adds manager.
Adjusting manifest
Last time we already added permissions for INTERNET and ACCESS_NETWORK_STATE. Today we will add optional permissions for reading state of wifi and to roughly detect the location of the user.
<!-- additional Madvertise permissions -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Next we need to adjust the manifest file with Madvertise activity. There is also place for token identifying your app. If you just want to test then put word "TestTokn" there. Do not forget to replace it when you are ready to publish.
<activity android:name="de.madvertise.android.sdk.MadvertiseActivity" />
<!-- Setup your site token. For tests, use 'TestTokn' -->
<meta-data
android:name="madvertise_site_token"
android:value="TestTokn" />
Adjusting constants
When implementing AdMob we had to adjust file SBCAdsConstants.java with AdMob ad unit ID. This is not necessary here as we put our Madvertise ad ID directly into manifest file.
MadvertiseSystem.java
Now we are ready to implement the class. We start with imports and variables again:
package com.sbcgames.sbcads;
import com.sbcgames.sbcengine.SBCEngine;
import de.madvertise.android.sdk.MadvertiseView;
import de.madvertise.android.sdk.MadvertiseView.MadvertiseViewCallbackListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
public class MadvertiseSystem implements SBCAdSystem, MadvertiseViewCallbackListener
{
private final String SYSTEM_NAME = "MADVERTISE ";
private SBCAdEventListener mListener = null;
private MadvertiseView mAdView;
private ViewGroup mLayout;
Instead of AdMob AdListener we are implementing MadvertiseViewCallbackListener this time. SBCAdSystem is our interface that we have to implement for every ad network we want our ad manager to handle. This is the constructor:
// ------------------------------------------------------------------------
public MadvertiseSystem(SBCEngine aSBCEngine, ViewGroup aLayout)
{
mLayout = aLayout;
// new view - all defaults
mAdView = new MadvertiseView(aSBCEngine.getApplicationContext());
// set the AdListener.
mAdView.setMadvertiseViewCallbackListener(this);
// Add the AdView to the view hierarchy. The view will have no size until the ad is loaded.
aLayout.addView(mAdView);
}
SBCAdSystem implementation
To start Madvertise ad network we have to implement start method from SBCAdSystem interface:
//------------------------------------------------------------------------
@Override
public void start()
{
mAdView.setVisibility(View.VISIBLE);
// Start loading the ad in the background.
mAdView.setFetchingAdsEnabled(true);
}
Calling setFetchingAdsEnabled with true will launch our request for ad. With the result of this reqest we will be notified through MadvertiseViewCallbackListener methods.
Next we need methods to stop and destroy this ad system in case we are switching to another one.
//------------------------------------------------------------------------
@Override
public void stop()
{
if (mAdView != null)
{
mAdView.setFetchingAdsEnabled(false);
mAdView.setVisibility(View.GONE);
}
}
//------------------------------------------------------------------------
@Override
public void destroy()
{
if (mAdView != null)
{
mAdView.removeMadViewCallbackListener();
}
mLayout.removeAllViews();
}
Finally there is method for setting the events listener and for processing keys. The later will again have no implementation for Madvertise:
//------------------------------------------------------------------------
@Override
public void setEventListener(SBCAdEventListener aListener)
{
mListener = aListener;
}
//------------------------------------------------------------------------
@Override
public boolean keyDown(final int aKeyCode, final KeyEvent aEvent)
{
return false;
}
MadvertiseViewCallbackListener implementation
Now comes the implementation of the interface that is part of Madvertise SDK. Again it is very simple as most of the methods will just notify our listener (that means our ad manager that is implementing it) and the rest is more or less just some debug output to watch in LogCat if everything is going well.
//------------------------------------------------------------------------
// MadvertiseViewCallbackListener
//------------------------------------------------------------------------
@Override
public void onLoaded(boolean succeed, MadvertiseView madView)
{
Log.d("AdsSystem", SYSTEM_NAME + "onLoaded");
if (!succeed)
{
mListener.onFailed(this);
}
}
//------------------------------------------------------------------------
@Override
public void onError(Exception exception)
{
Log.d("AdsSystem", SYSTEM_NAME + "onError");
mListener.onFailed(this);
}
//------------------------------------------------------------------------
@Override
public void onIllegalHttpStatusCode(int statusCode, String message)
{
Log.d("AdsSystem", SYSTEM_NAME + "onIllegalHttpStatusCode");
}
//------------------------------------------------------------------------
@Override
public void onAdClicked()
{
Log.d("AdsSystem", SYSTEM_NAME + "onAdClicked");
}
//------------------------------------------------------------------------
@Override
public void onApplicationPause()
{
Log.d("AdsSystem", SYSTEM_NAME + "onApplicationPause");
}
//------------------------------------------------------------------------
@Override
public void onApplicationResume()
{
Log.d("AdsSystem", SYSTEM_NAME + "onApplicationResume");
}
Conclusion
Today we added next ad network to our ad manager system. Next time we will ad Leadbolt network. You can now adjust the constants in SBCAdsConstants.java file so it handles two networks and with probability settings you can say how many percent of your users will ask for AdMob or Madvertise ad.