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.