AdMob libGDX with Google Play services

2019-05-21 10:11发布

问题:

Within the past few days google is forcing the use of the Google Play Services Mobile ads as the next version of android adMob. Im completely new to admob and am trying to test ads in my libGDX android game, but cant figure out how to do it because I get a rediculous amount of errors no matter what i do. I HAVE LOOKED AT OTHER EXAMPLES, but they are all for adMob 6.4.1(or earlier) which is now considered legacy. I just want an ad banner at the bottom of the screen but cant figure out how to do it. Could someone please post what my android MainActivity would need to be as well as anything I need to do to the manifest and xml.

It NEEDS to work with libGDX and however the view system would cooperate with that

Thank you very much!

回答1:

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.



回答2:

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.



回答3:

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.