简体   繁体   中英

Adding Admob to 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);

Nothing is shown, no ad, why, I added also in build.gradle (Project:projectN) compile "com.google.android.gms:play-services-ads:$admobVersion"

Using ubuntu 16.04, android-studio

Add AdMob Ads without Firebase :

  1. Put these lines inside build.gradle of android module.

     dependencies { compile 'com.google.android.gms:play-services-ads:10.2.4' } 
  2. Add permission in AndoidManifest.xml file

     <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    Inside <application tag add Activity if want to use Interstitial Ads

     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:theme="@android:style/Theme.Translucent" /> 
  3. AndroidLauncher class.

     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(); } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM