Last active
August 29, 2015 13:56
-
-
Save samschooler/9084428 to your computer and use it in GitHub Desktop.
MainActivity.java, implementing AdMob via MoPub with LibGDX. See blog post for more info: https://sam.ink/2014/implementing-admob-in-robovm-via-mopub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AndroidApplication { | |
MoPubView mAdView; | |
boolean adLoaded = false; | |
boolean adShown = false; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
RelativeLayout layout = new RelativeLayout(this); | |
requestWindowFeature(Window.FEATURE_NO_TITLE); | |
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, | |
WindowManager.LayoutParams.FLAG_FULLSCREEN); | |
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); | |
MyExampleGame game = new MyExampleGame(); | |
ShowAdListener show = new ShowAdListener(); | |
HideAdListener hide = new HideAdListener(); | |
show.setBase(this); | |
hide.setBase(this); | |
game.addListener(show); | |
game.addListener(hide); | |
View gameView = initializeForView(game, true); | |
layout.addView(gameView); | |
mAdView = new MoPubView(this); | |
mAdView.setAdUnitId("00000000000000000000000000000"); // Your MoPub Ad Unit ID | |
RelativeLayout.LayoutParams adParams = | |
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, | |
RelativeLayout.LayoutParams.WRAP_CONTENT); | |
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); | |
adParams.addRule(RelativeLayout.CENTER_IN_PARENT); | |
layout.addView(mAdView, adParams); | |
setContentView(layout); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
} | |
@Override | |
public void onDestroy() { | |
mAdView.destroy(); | |
super.onDestroy(); | |
} | |
public void showAd() { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
if(!adLoaded) { | |
System.out.println("Ad Loaded!"); | |
mAdView.loadAd(); | |
adLoaded = true; | |
} | |
if(!adShown) { | |
System.out.println("Ad Show!"); | |
mAdView.setVisibility(View.VISIBLE); | |
adShown = true; | |
} | |
} | |
}); | |
} | |
public void hideAd() { | |
if(adShown) { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
mAdView.setVisibility(View.GONE); | |
System.out.println("Ad Hide!"); | |
adShown = false; | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment