Home

demiremre edited this page Dec 7, 2016 · 12 revisions
Clone this wiki locally

Include GDApi using Android Studio

There are two required files to use our GDApi:

  • gdapi.jar
  • google play services

Adding gdapi.jar and Google play services as dependency

Firstly, as you can see at the picture below, after downloading gdapi.jar file from our page, it is supposed to be placed into "libs" folder under your project. Copy gdapi.jar file and paste it into "libs" folder.

Secondly and finally, the library is supposed to be included and compiled at "build.gradle". There are two different ways to do it. The pictures below illustrate how to do it.

How to use it

Initialize

There are three parameters required to invoke init function: Game id, user id and activity context which is required to show the ad in.

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.gd.analytics.GDlogger;

public class MainActivity extends AppCompatActivity {

    Activity mContext;
    String gameId,userId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gameId = "xxxxxx";
        userId = "xxxxxx";
        mContext = this;  // context where the user wants to serve the ad in

        GDlogger.debug(true);  // enables log messages related to api at logcat (android monitor)

        GDlogger.init(gameId,userId,mContext); // enables


        /*
            If pre-roll ad is enable on your account, init shows an interstitial ad as well

         */

    }
}

Request Ad

There are two types ads which the developer can request for: Interstitial and banner ads.

Interstitial ads are full screen ads that cover the interface of their host application. They're typically displayed at natural transition points in the flow of an application, such as between activities or during the pause between levels in a game.

Banner ads are rectangular graphic display that stretches across bottom of a website.

The picture below demonstrates how to request for banner and interstitial ads using GDApi. It uses ShowBanner() function to request and the function gets parameter as a json string.

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.gd.analytics.GDlogger;

public class MainActivity extends AppCompatActivity {
    Activity mContext;
    String gameId,userId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gameId = "xxxxxx";
        userId = "xxxxxx";
        mContext = this;  // context where the user wants to serve the ad in

        GDlogger.debug(true);  // enables log messages related to api at logcat (android monitor)

        GDlogger.init(gameId,userId,mContext); // enables


        /*
            If pre-roll ad is enable on your account, init shows an interstitial ad as well

         */

        Boolean isInterstitial = false;
        GDlogger.ShowBanner(isInterstitial); // request for a standart banner ad, 320x50, at bottom center

        // It is possible to specify size, alignment and position of the banner ad.
        GDlogger.ShowBanner(GDAdSize.BANNER, GDAlignment.CENTER, GDAdPosition.BOTTOM);

       /*
          Alignment: LEFT, CENTER, RIGHT
          Position: TOP, MIDDLE, BOTTOM
       */

        isInterstitial = true;
        GDlogger.ShowBanner(isInterstitial); // request for a interstitial ad

        /*
            ShowBanner() can not be invoked for one ad type consecutively. Time limitation can be set on the user's account.
            For instance; you can not invoke ShowBanner for banner again within time limit if it has been already used for a banner request. 
            However, you can request for an interstitial.
            If pre-roll is enabled, init function also requests for an interstitial.
         */

    }
}

Events

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.gd.analytics.GDEvent;
import com.gd.analytics.GDadListener;
import com.gd.analytics.GDlogger;

public class MainActivity extends AppCompatActivity {

    Activity mContext;
    String gameId,userId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gameId = "xxxxxx";
        userId = "xxxxxx";
        mContext = this;  // context where the user wants to serve the ad in

        GDlogger.debug(true);  // enables log messages related to api at logcat (android monitor)

        GDlogger.init(gameId,userId,mContext); // enables

        GDlogger.setAdListener(new GDadListener() {
            @Override
            public void onBannerClosed() {
                super.onBannerClosed();
            }

            @Override
            public void onBannerStarted() {
                super.onBannerStarted();
            }

            @Override
            public void onBannerRecieved(GDEvent data) {
                super.onBannerRecieved(data);
            }
        });

        GDlogger.ShowBanner(false); // request for a standart banner ad 320x50 at bottom center

    }
}