Skip to content

Instantly share code, notes, and snippets.

@foobraco
Last active October 6, 2015 11:08
Show Gist options
  • Save foobraco/2984893 to your computer and use it in GitHub Desktop.
Save foobraco/2984893 to your computer and use it in GitHub Desktop.
android code for getting ads to work in playn
mvn install:install-file \
-DgroupId=com.yourname.jgoodies \
-DartifactId=jgoodies-forms \
-Dversion=1.50 \
-Dfile=/path/to/jgoodies-1.50.jar \
-Dpackaging=jar \
-Durl=file://path/to/your/local/repository
public class MyGameActivity extends GameActivity{
protected AdView adView;
public Vibrator vib;
public boolean adsactive;
AlertDialog.Builder builder;
MyGame game;
AdWhirlLayout adWhirlLayout;
Intent share;
public class AndroidAds implements MyGame.myAds {
@Override
public void activateAds() {
runOnUiThread(new Runnable() {
@Override
public void run() {
adView.setVisibility(View.VISIBLE);
adView.startLayoutAnimation();
}
});
}
@Override
public void deactivateAds() {
runOnUiThread(new Runnable() {
@Override
public void run() {
adWhirlLayout.setVisibility(View.GONE);
adView.setVisibility(View.GONE);
}
});
}
@Override
public void refreshAds() {
runOnUiThread(new Runnable(){
@Override
public void run() {
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView.startLayoutAnimation();
adView.loadAd(adRequest);
}
});
}
}
@Override
public void onBackPressed() {
// This method controls what happens when the back button is pressed
MyGame.backPressed = true;
// moveTaskToBack(false);
}
@Override
public void main() {
game = new MyGame(new AndroidAds());
share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
builder = new AlertDialog.Builder(this);
new AlertDialog.Builder(this);
vib = (Vibrator) getSystemService(VIBRATOR_SERVICE);
platform().graphics().registerFont("Text/PressStart2P.ttf",
"PressStart2P", Font.Style.PLAIN);
platform().graphics().registerFont("Text/PressStart2P.ttf",
"PressStart2P", Font.Style.BOLD_ITALIC);
PlayN.run(game);
}
public static void setLayoutAnim_slidedownfromtop(ViewGroup panel,
Context ctx) {
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(100);
set.addAnimation(animation);
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
-1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(500);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(
set, 0.25f);
panel.setLayoutAnimation(controller);
}
public void createAdview(RelativeLayout rl) {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rlp2.addRule(RelativeLayout.CENTER_IN_PARENT);
rl.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
adView = new AdView(this, AdSize.BANNER, "a150d4f3038d939");
adView.setId(234);
this.gameView().setBackgroundColor(Color.TRANSPARENT);
if(this.gameView()!=null){
rl.addView(adView, rlp2);
rl.bringToFront();
rl.bringChildToFront(adView);
setLayoutAnim_slidedownfromtop(adView,this);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(adRequest);
adView.setVisibility(View.GONE);
}
this.gameView().setBackgroundColor(Color.TRANSPARENT);
rl.forceLayout();
}
@Override
protected void setContentView(GameViewGL view) {
final RelativeLayout layout = new RelativeLayout(this);
view.setKeepScreenOn(true);
this.runOnUiThread(new Runnable(){
@Override
public void run() {
createAdview(layout);
adView.setAdListener(new AdListener(){
@Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
}
@Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
@Override
public void onReceiveAd(Ad arg0) {
adView.bringToFront();
}
});
layout.setBackgroundColor(0xFF000000);
layout.setGravity(Gravity.CENTER);
layout.addView(gameView());
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
getWindow().setContentView(layout, params);
}
});
}
}
public interface myAds {
void activateAds();
void deactivateAds();
}
public final myAds myads;
public MyGame(myAds myads) {
this.myads = myads;
}
<dependency>
<groupId>com.admob</groupId>
<artifactId>admob</artifactId>
<version>6.0</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment