Integrating AdMod intertitials

Hello, I have been trying to get interstitial ads working on my android app. I have searched the forum + internet and there are one or two posts by people trying to figure out how to get ads working, which have been helpful but were mostly related to banner ads (which seem to work differently) or were inconclusive, so I was wondering if anyone around on the forum has successfully implemented interstitials.
What I have done so far:

  • Downloaded google play services from the sdk
  • Copied the google_play_services_lib project to the jmonkey libs directory
  • Opened and compiled that project
  • Referenced the library that that created in my jmonkey game
  • Changed my AndroidManifest to include the permissions AdMod needs as per google’s tutorials
  • Copied the version.xml from google_play_services project res/values folder into mine

What I’m stuck with:

  • I’ve added code as in google’s latest documentation (see below) but my game starts as usual with no ads
  • Finding my devices hashed md5 id so I can test ads on my device

This is google’s MainActivity example

public class MainActivity extends ActionBarActivity {

InterstitialAd mInterstitialAd;
Button mNewGameButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNewGameButton = (Button) findViewById(R.id.newgame_button);

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();
            beginPlayingGame();
        }
    });

    requestNewInterstitial();

    mNewGameButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            } else {
                beginPlayingGame();
            }
        }
    });

    beginPlayingGame();
}

private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
              .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
              .build();

    mInterstitialAd.loadAd(adRequest);
}

private void beginPlayingGame() {
    // Play for a while, then display the New Game Button
}
}

This is what I have tried in my MainActivity (I can’t get the preformatted text working for this one):

package com.renewablegames.duckyinspace;

import Main.Main;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import com.jme3.app.AndroidHarness;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.system.android.AndroidConfigChooser.ConfigType;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.LogManager;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.AdListener;

public class MainActivity extends AndroidHarness implements SensorEventListener {

private SensorManager sensorManager;
private Main main;
private float[] acc = {0, 0, 0};
private InterstitialAd mInterstitialAd;

public MainActivity() {
    // Set the application class to run
    appClass = "Main.Main";
    // Try ConfigType.FASTEST; or ConfigType.LEGACY if you have problems
    eglConfigType = ConfigType.BEST;
    // Exit Dialog title & message
    exitDialogTitle = "Quit Game?";
    exitDialogMessage = "";
    // Enable verbose logging
    eglConfigVerboseLogging = false;
    // Choose screen orientation
    screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    // Enable MouseEvents being generated from TouchEvents (default = true)
    mouseEventsEnabled = false;
    // Set the default logging level (default=Level.INFO, Level.ALL=All Debug Info)
    LogManager.getLogManager().getLogger("").setLevel(Level.OFF);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();
            beginPlayingGame();
        }
    });

    requestNewInterstitial();

}

private void beginPlayingGame() {
    main = (Main) this.getJmeApplication();
}

private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
            .build();

    mInterstitialAd.loadAd(adRequest);
}

@Override
protected void onResume() {
    super.onResume();
    sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onStop() {
    sensorManager.unregisterListener(this);
    super.onStop();
}

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    if (main == null) {
        return;
    }
    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        acc = sensorEvent.values.clone();



        main.enqueue(new Callable() {
            public Object call() throws Exception {
                main.walkDirection = new Vector3f(-acc[0] * 2.3f, 0, 0);
                return null;
            }
        });
    }
}
}

So yeah, if anyone knows whats what then some help would be nice. Thank you for your time.

Heres an idea if you create a binding interface in your jMonkey Project

public interface AdModBinding {
	
	public void requestNewInterstitial();
	
	public void showInterstitial();
}

Then you could create an instance of it in your Android Project

public class MyAdModBinding implements AdModBinding {
	
	private InterstitialAd interstitialAd;
	
	public MyAdModBinding(InterstitialAd interstitialAd) {
		this.interstitialAd = interstitialAd;
	}
	
	public void requestNewInterstitial() {
		AdRequest adRequest = new AdRequest.Builder()
			.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
			.build();
		
		interstitialAd.loadAd(adRequest);
	}
	
	public void showInterstitial() {
		if (interstitialAd.isLoaded()) {
			interstitialAd.show();
		}
	}
}

and finally make your MyGame application use it.

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	// 
	// your code for creating interstitialAd here
	// 
	MyAdModBinding myAdModBinding = new MyAdModBinding(mInterstitialAd);
	((Main.Main)app).setAdModBinding(myAdModBinding);
}

Of course you will need to add a method in your jMonkey application to setAdModBinding

So end result from your jMonkeyEngine main application you can now force it to show your ad.

this is what I am planning to do I have done about as much testing as you so how about I give you the idea and you tell me if it works.

1 Like

If you can get this to work I’d be super interested.

I could never get admob interstitials working but I did get them to work on other ad sdk’s that I tested out.

I currently rely on banners but I would like to see if someone has successfully gotten through this.

I believe @zzuegg said he had some success with this if I recall.

I just noticed I think you are saying you cant get the google example to work?

How Can I Get Device ID for AdMob covers using emulators and devices. InterstitialExample I find that reading source is easier to understand that most tutorials.

Anyway good luck!

A skidrunner thanks for the idea and the links, i’m having trouble working like an interface like that but I’m going to keep working at it over the next few days, will post again here if I start getting anywhere

@BigBob, @skidrunner, @crunched I am working on a sample for you guys on how to show interstitial ads.
Just give me a few hours, not at my PC now.

1 Like

Okay here is it.
For those who want a way to add Admob Interstitials to there projects or games here is a link to my solution.
It works perfectly with no problems what so ever.
https://drive.google.com/file/d/0BwsJr-qW02qRaElrSzJrRkhSTjQ/view?usp=sharing
This example is a jME3.0 project. Unzip it and open it up in jMonkeyPlatform.

1 Like

@BigBob, @skidrunner @crunched did you guys find this solution useful in anyway?

1 Like

I haven’t had time to check it out, was working on physics particles…lol but I’ll check it out and give you a :heart: if it works…lol

It works man! I’ve been busy the last few days but I’m very impressed! Thanks so much! one thing I am curious about is how to give the app the google play services version without hardcoding the value in my res folder… I had to comment out the android.library.reference.1=…/…/google-play-services_lib line in your project.properties and hardcode a value for my google play services version to get it to work. Anyway, thanks alot for your help.

Well yeah you have to change the location of your play service path.
So basically that is it.
I have release a few games on android and almost all uses admob.
I have a couple of years of experience with the integration between jME and android.

Here is al my games: https://play.google.com/store/apps/developer?id=bruynhuis

Thanks very much, I’ve seen your new game and I’ll definitely be checking it out, looks awesome man

1 Like