Amazon Mobile Banner Ads in Cocos2D-X on Android

So I’m releasing an new free version of  Arrow Mania (Free) on Amazon so it only seems polite to include Amazon Ads. Plus there is a “free kindle fire HD” promotion on until Sept 1st so worth a try to win one!

Amazon Ads in Arrow Mania
Amazon Ads in Arrow Mania

It took me a few hours to figure out how to get the ads to display so I thought I’d share the knowledge. First, make sure you have downloaded the SDK from Amazon and locate the FloatingAdSample folder. From the sample add its “anim” folder to your projects res, it is located here “Apps-SDK/Android/Ads/samples/FloatingAdSample/res/anim”. In Eclipse, just drag and drop the folder to your projects “res” folder and select “copy files” from the dialog.

In your Manifest file, add the activity below and the permissions (see Amazons page for more detail if you don’t know how):

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<activity
            android:name="com.amazon.device.ads.AdActivity"
            android:configChanges="keyboardHidden|orientation|screenSize">
        </activity>

									

 

In your Cocos2dxActivity Java file:-

Make sure you have all the following imports:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import android.view.*;
import android.view.animation.*;
import android.view.animation.Animation.AnimationListener;
import android.widget.FrameLayout.LayoutParams;

import com.amazon.device.ads.*;

import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;
									

Add this code to your OnCreate. This creates the banner view top middle of the screen.

      LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
                getWindowManager().getDefaultDisplay().getWidth(),
                  -50);
            LinearLayout subLayout = new LinearLayout(this);
            subLayout.setOrientation(LinearLayout.VERTICAL);
            subLayout.setBackgroundColor(Color.TRANSPARENT);
            subLayout.setGravity(Gravity.CENTER);
            addContentView(subLayout, adParams);
      adViewContainer = subLayout;

          // For debugging purposes enable logging, but disable for production builds
          AdRegistration.enableLogging(true);
          // For debugging purposes flag all ad requests as tests, but set to false for production builds
          AdRegistration.enableTesting(true);

          try {
              AdRegistration.setAppKey(APP_KEY);
          } catch (Exception e) {
              Log.e(LOG_TAG, "Exception thrown: " + e.toString());
              return;
          }

          LoadAd();
									

Copy and paste the handler functions, these also do the banner animations so have a play if you want the change the animation direction from up to down, etc. Most of this code is taken directly from the FloatingAdSample.

[codebox 4]

 

Add to your OnDestroy() function

          if (currentAdView != null)
              this.currentAdView.destroy();
          if (nextAdView != null)
              this.nextAdView.destroy();