Permalink
Browse files

Implemented Interstitial

  • Loading branch information...
MrDT MrDT
MrDT authored and MrDT committed Apr 18, 2014
1 parent 9a4c934 commit 0a5ea376d4eb92b8e87c13a03245adb40b53e811
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
- <string name="app_name">My LibGDX Game</string>
+ <string name="app_name">Google Ads Tutorial</string>
</resources>
@@ -13,19 +13,24 @@
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
+import android.widget.Toast;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
+import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
+import com.google.android.gms.ads.InterstitialAd;
-public class MainActivity extends AndroidApplication {
+public class MainActivity extends AndroidApplication implements ActionResolver {
- private static final String AD_UNIT_ID = "ca-app-pub-6916351754834612/3101802628";
+ private static final String AD_UNIT_ID_BANNER = "ca-app-pub-6916351754834612/9855033027";
+ private static final String AD_UNIT_ID_INTERSTITIAL = "ca-app-pub-6916351754834612/3808499421";
private static final String GOOGLE_PLAY_URL = "https://play.google.com/store/apps/developer?id=TheInvader360";
protected AdView adView;
protected View gameView;
+ private InterstitialAd interstitialAd;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -52,12 +57,25 @@ public void onCreate(Bundle savedInstanceState) {
setContentView(layout);
startAdvertising(admobView);
+
+ interstitialAd = new InterstitialAd(this);
+ interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
+ interstitialAd.setAdListener(new AdListener() {
+ @Override
+ public void onAdLoaded() {
+ Toast.makeText(getApplicationContext(), "Finished Loading Interstitial", Toast.LENGTH_SHORT).show();
+ }
+ @Override
+ public void onAdClosed() {
+ Toast.makeText(getApplicationContext(), "Closed Interstitial", Toast.LENGTH_SHORT).show();
+ }
+ });
}
private AdView createAdView() {
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
- adView.setAdUnitId(AD_UNIT_ID);
+ adView.setAdUnitId(AD_UNIT_ID_BANNER);
adView.setId(12345); // this is an arbitrary id, allows for relative positioning in createGameView()
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
@@ -68,7 +86,7 @@ private AdView createAdView() {
}
private View createGameView(AndroidApplicationConfiguration cfg) {
- gameView = initializeForView(new AdTutorial(), cfg);
+ gameView = initializeForView(new AdTutorial(this), cfg);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
@@ -81,7 +99,27 @@ private void startAdvertising(AdView adView) {
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
-
+
+ @Override
+ public void showOrLoadInterstital() {
+ try {
+ runOnUiThread(new Runnable() {
+ public void run() {
+ if (interstitialAd.isLoaded()) {
+ interstitialAd.show();
+ Toast.makeText(getApplicationContext(), "Showing Interstitial", Toast.LENGTH_SHORT).show();
+ }
+ else {
+ AdRequest interstitialRequest = new AdRequest.Builder().build();
+ interstitialAd.loadAd(interstitialRequest);
+ Toast.makeText(getApplicationContext(), "Loading Interstitial", Toast.LENGTH_SHORT).show();
+ }
+ }
+ });
+ } catch (Exception e) {
+ }
+ }
+
@Override
public void onResume() {
super.onResume();
@@ -0,0 +1,9 @@
+package com.theinvader360.tutorial.libgdx.google.ads;
+
+public class ActionResolverDesktop implements ActionResolver {
+
+ @Override
+ public void showOrLoadInterstital() {
+ System.out.println("showOrLoadInterstital()");
+ }
+}
@@ -11,6 +11,6 @@ public static void main(String[] args) {
cfg.width = 480;
cfg.height = 320;
- new LwjglApplication(new AdTutorial(), cfg);
+ new LwjglApplication(new AdTutorial(new ActionResolverDesktop()), cfg);
}
}
@@ -0,0 +1,5 @@
+package com.theinvader360.tutorial.libgdx.google.ads;
+
+public interface ActionResolver {
+ public void showOrLoadInterstital();
+}
@@ -9,14 +9,22 @@
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
+import com.badlogic.gdx.input.GestureDetector;
+import com.badlogic.gdx.input.GestureDetector.GestureListener;
+import com.badlogic.gdx.math.Vector2;
-public class AdTutorial implements ApplicationListener {
+public class AdTutorial implements ApplicationListener, GestureListener {
+ private ActionResolver actionResolver;
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
- @Override
+ AdTutorial(ActionResolver actionResolver) {
+ this.actionResolver = actionResolver;
+ }
+
+ @Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
@@ -33,6 +41,9 @@ public void create() {
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
+
+ GestureDetector gd = new GestureDetector(this);
+ Gdx.input.setInputProcessor(gd);
}
@Override
@@ -52,6 +63,12 @@ public void render() {
batch.end();
}
+ @Override
+ public boolean tap(float x, float y, int count, int button) {
+ actionResolver.showOrLoadInterstital();
+ return true;
+ }
+
@Override
public void resize(int width, int height) {
}
@@ -63,4 +80,45 @@ public void pause() {
@Override
public void resume() {
}
+
+ @Override
+ public boolean touchDown(float x, float y, int pointer, int button) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public boolean fling(float velocityX, float velocityY, int button) {
+ return false;
+ }
+
+ @Override
+ public boolean longPress(float x, float y) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public boolean pan(float x, float y, float deltaX, float deltaY) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public boolean panStop(float x, float y, int pointer, int button) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public boolean zoom(float initialDistance, float distance) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
+ // TODO Auto-generated method stub
+ return false;
+ }
}

0 comments on commit 0a5ea37

Please sign in to comment.