将Admob添加到libgdx

RelativeLayout layout = new RelativeLayout(this); AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); View gameView = initializeForView(new MainGame(), config); layout.addView(gameView); adView = new AdView(this); adView.setAdListener(new AdListener() { @Override public void onAdLoaded() { System.out.println("LOAD"); } }); adView.setAdSize(AdSize.SMART_BANNER); adView.setAdUnitId("ca-app-xxx-xxxxxxxxxx/xxxxxxxxxx"); AdRequest.Builder builder = new AdRequest.Builder(); RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT ); layout.addView(adView, adParams); adView.loadAd(builder.build()); setContentView(layout); 

没有显示任何内容,没有广告,为什么,我也在build.gradle中添加(Project:projectN)编译“com.google.android.gms:play-services-ads:$ admobVersion”

使用ubuntu 16.04,android-studio

添加没有Firebase的AdMob广告:

  1. 把这些行放在android模块的build.gradle中。

     dependencies { compile 'com.google.android.gms:play-services-ads:10.2.4' } 
  2. AndoidManifest.xml文件中添加权限

       

    如果要使用非页内广告,请在标记内添加Activity

       
  3. AndroidLauncher类。

     public class AndroidLauncher extends AndroidApplication { private static final String adUnitId="ca-app-pub-xxxxxxxxxxxxxxxxxxxxx"; private AdView adView; @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); RelativeLayout layout = new RelativeLayout(this); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); layout.setLayoutParams(params); View gameView=initializeForView(new MyGdxGame(), config); RelativeLayout.LayoutParams gameViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); gameViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); gameViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); gameView.setLayoutParams(gameViewParams); layout.addView(gameView); adView = new AdView(this); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId(adUnitId); AdRequest.Builder adRequestBuilder = new AdRequest.Builder(); adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR); adView.loadAd(adRequestBuilder.build()); RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); topParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE); topParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); layout.addView(adView, topParams); adView.setBackgroundColor(android.graphics.Color.TRANSPARENT); setContentView(layout); } @Override protected void onResume() { super.onResume(); adView.resume(); } @Override protected void onPause() { super.onPause(); adView.pause(); } @Override protected void onDestroy() { super.onDestroy(); adView.destroy(); } }