How to – integrate Banner Ads Into Your Application

by BlackBerry Development Advisor on ‎09-27-2010 06:25 PM (14,335 Views)

The BlackBerry Advertising Service allows you to easily integrate banner ads into your application.  I’m going to put together a quick sample application to show you just how easy it is.  First, I’ll start with a basic class that creates a screen and pushes it onto the display stack.

 

 

import net.rim.blackberry.api.advertising.app.Banner;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;

/**
 * A simple sample application that demonstrates the use of the banner class
 * from the Advertisement SDK. A sample Application Placement ID is used to
 * demonstrate the simplicity of the API.
 */
final class AdSample extends UiApplication
{

    /**
     * This is an Application ID (APID) that is used solely for demonstration
     * purposes.  For your application you will need to use the one that will be assigned to you after registration.
     */
    private static final int APID = 0;
    
    public static void main(String[] args)
    {
        AdSample app = new AdSample();
        app.enterEventDispatcher();
    }

    public AdSample()
    {
        DemonstrationScreen demoScreen = new DemonstrationScreen();
        pushScreen(demoScreen);
    }

    private class DemonstrationScreen extends MainScreen
    {
	//Ad code goes here
}
}

 

 

Now that we have our simple application it’s time to our banner advertisement.  Let’s place a banner ad into the DemonstrationScreen screen using its constructor:

 

 

  public DemonstrationScreen()
        {
  //Create a banner object by passing it the demo application placement ID and null since we don’t have any meta data to pass.
            Banner bannerAd = new Banner(AdSample.APID, null);
            //Sets the size of the banner ad that specified in the bannerSize integer value.
            bannerAd.setMMASize(Banner.MMA_SIZE_AUTO);
            //Adds the banner ad to the screen
            add(bannerAd);
        }

 

That’s it! All the networking and rendering functionality is handled by the banner class so all that you are required to do is place it.