AdMob libGDX with Google Play services

老子叫甜甜 提交于 2019-12-06 05:08:53

Follow the official guide on migrating to the new admob here. Then follow the admob in libgdx wiki guide to complete the migration. It's really simple.

The changes you'll need to make in your MainActivity class are:

Change the lines:

 AdView adView = new AdView(this, AdSize.BANNER, "xxxxxxxx"); // Put in your secret key here
      adView.loadAd(new AdRequest());

to:

AdView adView = new AdView(activity);
adView.setAdUnitId("xxxxxxx");
adView.setAdSize(AdSize.BANNER);
adView.loadAd(new AdRequest.Builder()
.build());

Additionally, since you want the ad to appear at the bottom of the screen, modify the adParams as follows:

RelativeLayout.LayoutParams adParams = 
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

And in your manifest file,

Change:

<activity android:name="com.google.ads.AdActivity"/>

to:

<activity android:name="com.google.android.gms.ads.AdActivity" 
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>`

<meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version"/>

You don't need to define the ad view in an xml layout since it's already done programmatically in the MainActivity Class. You can also implement the AdListener to get listen for the Ad callbacks.

Please follow the inmstruction given to the google official website

https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals

Do Add These Line In The MainActivity.java

  /** The view to show the ad. */
  private AdView adView;

  /* Your ad unit id. Replace with your actual ad unit id. */
  private static final String AD_UNIT_ID = "INSERT_YOUR_AD_UNIT_ID_HERE";

Add these lines on Activity OnCreate..

    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
    layout.addView(adView);

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
        .build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);

Also Add some lines in Manifest.xml

    <activity android:name="com.google.android.gms.ads.AdActivity"  
                            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

    <meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

Now add permissions...

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

And also don't forgot to add google-play-services as library project.

Hope this will help you.

The diference between the new admob and the old one is just that key..

I used this tutorial https://code.google.com/p/libgdx/wiki/AdMobInLibgdx And put my new key and works good, and I recieve the statistics, clicks count, etc.

I didn't installed google play service to my app.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!