J2ME SDK

From Adfonic Wiki
Jump to: navigation, search

Getting Started

To get live ads in your app you just need to:

Configuring the J2ME SDK

  1. The Adfonic J2ME SDK uses the Glassbox open source Java ME library. For more information please see the Glassbox project page
  2. Once downloaded, ensure the Glassbox is properly installed in your JavaME project.
  3. The code below shows a fully functional sample MIDlet class with your Ad Slot ID embedded. You will need to adapt this to your own interface logic.
import la.pandora.mobile.util.*;
import la.pandora.mobile.model.*;
import la.pandora.mobile.service.*;

import java.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class AdfonicSampleMIDlet extends MIDlet implements TaskCallback, CommandListener {

    // the ad
    private AdfonicResponse ad;

    // our display
    private Form f = new Form("Adfonic Sample");

    public void startApp() {
        f.setCommandListener(this);
        f.addCommand(new Command("exit", Command.EXIT, 1));

        Display d = Display.getDisplay(this);

        // set our form to display now
        d.setCurrent(f);

        // show some loading activity
        f.append(new Gauge("requesting ad...", false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING));

        // start the async adfonic service
        // using this midlet as the callback

        //TaskOperator.prepare(new AdfonicService(22222222-2222-2222-2222-222222222222, null, null, null), this);

        // the age, gender (m|f) and additional tags parameters are all optional
        //TaskOperator.prepare(new AdfonicService(22222222-2222-2222-2222-222222222222, "28", "m", "comma,separated,tags"), this);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean b) {
        notifyDestroyed();
    }

    /**
     * Handle our key events
     */
    public void commandAction(Command c, Displayable d) {
        if (c.getCommandType() == Command.EXIT) {
            notifyDestroyed();
        }
        else if (c.getCommandType() == Command.ITEM) {
            try {
                if (ad != null)
                    // open the ad link in the browser
                    platformRequest(ad.linkUrl);
            }
            catch (Exception ignore) {}
        }
    }

    /**
     * This is the callback from the Adfonic Service
     *
     * @param TaskResult the wrapper object that has the response from the service
     */
    public void taskCallback(TaskResult result) {
        AdfonicService svc = (AdfonicService)result.getTask();

        if (result.getResultStatus() == TaskOperator.STATUS_SUCCESS) {
            ad = svc.getAd();

            f.deleteAll();
            if (ad != null) {
                // give us a way to interact
                f.addCommand(new Command("click ad", Command.ITEM, 2));

                // if we have the image, display it
                if (ad.image != null) {
                    f.append(ad.image);
                }

                // dump the values in our Adfonic ad to screen
                f.append(ad.toString());
            }
            else {
                f.append("No ad available for this phone, or at this time");
            }
        }
        else {
            /**
             * We do have access to other callback states, but we're not interested in those.
             *
             * TaskOperator.STATUS_SUCCESS
             * TaskOperator.STATUS_STARTLOADING
             * TaskOperator.STATUS_ENDLOADING
             * TaskOperator.STATUS_TIMEOUT
             * TaskOperator.STATUS_CANCELLED
             * TaskOperator.STATUS_ERROR
             */
        }
    }
}
Personal tools
Namespaces
Variants
Actions
App/Site Integration
Ad Campaign Tracking
Adfonic API
Troubleshooting
Toolbox