地址是:
http://code.google.com/p/libgdx/wiki/AdMobInLibgdx
package com .badlogic .gdx ;
import android .os . Bundle ;
import android .os . Handler ;
import android .os . Message ;
import android .view . View ;
import android .view . Window ;
import android .view . WindowManager ;
import android .widget . RelativeLayout ;
import com .badlogic .gdx .backends .android . AndroidApplication ;
import com .badlogic .gdx .helloworld . HelloWorld ;
import com .badlogic .gdx .helloworld . IActivityRequestHandler ;
import com .mobclix .android .sdk . MobclixMMABannerXLAdView ;
//HelloWorldAndroid用自己的入口类
public class HelloWorldAndroid extends AndroidApplication implements IActivityRequestHandler {
protected AdView adView ;
private final int SHOW_ADS = 1 ;
private final int HIDE_ADS = 0 ;
protected Handler handler = new Handler ()
{
@Override
public void handleMessage ( Message msg ) {
switch (msg .what ) {
case SHOW_ADS :
{
adView .setVisibility ( View .VISIBLE );
break ;
}
case HIDE_ADS :
{
adView .setVisibility ( View .GONE );
break ;
}
}
}
};
@Override public void onCreate ( Bundle savedInstanceState ) {
super .onCreate (savedInstanceState );
// Create the layout
RelativeLayout layout = new RelativeLayout ( this );
// Do the stuff that initialize() would do for you
requestWindowFeature ( Window .FEATURE_NO_TITLE );
getWindow ().setFlags ( WindowManager . LayoutParams .FLAG_FULLSCREEN ,
WindowManager . LayoutParams .FLAG_FULLSCREEN );
getWindow ().clearFlags ( WindowManager . LayoutParams .FLAG_FORCE_NOT_FULLSCREEN );
// Create the libgdx View
View gameView = initializeForView ( new HelloWorld ( this ), false );
// Create and setup the AdMob view
adView = new AdView ( this , AdSize .BANNER , "xxxxxxxx" ); // Put in your secret key here
adView .loadAd ( new AdRequest ());
// Add the libgdx view
layout .addView (gameView );
// Add the AdMob view
RelativeLayout . LayoutParams adParams =
new RelativeLayout . LayoutParams ( RelativeLayout . LayoutParams .WRAP_CONTENT ,
RelativeLayout . LayoutParams .WRAP_CONTENT );
adParams .addRule ( RelativeLayout .ALIGN_PARENT_TOP );
adParams .addRule ( RelativeLayout .ALIGN_PARENT_RIGHT );
layout .addView (adView , adParams );
// Hook it all up
setContentView (layout );
}
// This is the callback that posts a message for the handler
@Override
public void showAds ( boolean show ) {
handler .sendEmptyMessage (show ? SHOW_ADS : HIDE_ADS );
}
}
package com .badlogic .gdx ;
import android .os . Bundle ;
import android .os . Handler ;
import android .os . Message ;
import android .view . View ;
import android .view . Window ;
import android .view . WindowManager ;
import android .widget . RelativeLayout ;
import com .badlogic .gdx .backends .android . AndroidApplication ;
import com .badlogic .gdx .helloworld . HelloWorld ;
import com .badlogic .gdx .helloworld . IActivityRequestHandler ;
import com .mobclix .android .sdk . MobclixMMABannerXLAdView ;
//HelloWorldAndroid用自己的入口类
public class HelloWorldAndroid extends AndroidApplication implements IActivityRequestHandler {
protected AdView adView ;
private final int SHOW_ADS = 1 ;
private final int HIDE_ADS = 0 ;
protected Handler handler = new Handler ()
{
@Override
public void handleMessage ( Message msg ) {
switch (msg .what ) {
case SHOW_ADS :
{
adView .setVisibility ( View .VISIBLE );
break ;
}
case HIDE_ADS :
{
adView .setVisibility ( View .GONE );
break ;
}
}
}
};
@Override public void onCreate ( Bundle savedInstanceState ) {
super .onCreate (savedInstanceState );
// Create the layout
RelativeLayout layout = new RelativeLayout ( this );
// Do the stuff that initialize() would do for you
requestWindowFeature ( Window .FEATURE_NO_TITLE );
getWindow ().setFlags ( WindowManager . LayoutParams .FLAG_FULLSCREEN ,
WindowManager . LayoutParams .FLAG_FULLSCREEN );
getWindow ().clearFlags ( WindowManager . LayoutParams .FLAG_FORCE_NOT_FULLSCREEN );
// Create the libgdx View
View gameView = initializeForView ( new HelloWorld ( this ), false );
// Create and setup the AdMob view
adView = new AdView ( this , AdSize .BANNER , "xxxxxxxx" ); // Put in your secret key here
adView .loadAd ( new AdRequest ());
// Add the libgdx view
layout .addView (gameView );
// Add the AdMob view
RelativeLayout . LayoutParams adParams =
new RelativeLayout . LayoutParams ( RelativeLayout . LayoutParams .WRAP_CONTENT ,
RelativeLayout . LayoutParams .WRAP_CONTENT );
adParams .addRule ( RelativeLayout .ALIGN_PARENT_TOP );
adParams .addRule ( RelativeLayout .ALIGN_PARENT_RIGHT );
layout .addView (adView , adParams );
// Hook it all up
setContentView (layout );
}
// This is the callback that posts a message for the handler
@Override
public void showAds ( boolean show ) {
handler .sendEmptyMessage (show ? SHOW_ADS : HIDE_ADS );
}
}