AdMob libGDX 与 Google Play 服务

在过去的几天里,谷歌正在强制使用 Google Play Services 移动广告作为 android adMob 的下一个版本.我对 admob 完全陌生,正在尝试在我的 libGDX android 游戏中测试广告,但无法弄清楚如何去做,因为无论我做什么,我都会遇到大量错误.我看过其他示例,但它们都适用于现在被视为旧版的 adMob 6.4.1(或更早版本).我只想在屏幕底部放置一个广告横幅,但不知道该怎么做.有人可以发布我的 android MainActivity 需要什么以及我需要对清单和 xml 做的任何事情.

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.

它需要与 libGDX 一起工作,但是视图系统会与之合作

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

非常感谢!

推荐答案

关注 官方指南在这里迁移到新的 admob.然后按照 admob in libgdx wiki 指南 完成迁移.这真的很简单.

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.

您需要在 MainActivity 类中进行的更改是:

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

换行:

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

到:

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

另外,由于您希望广告出现在屏幕底部,请按如下方式修改 adParams:

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

在你的清单文件中,

变化:

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

到:

<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"/>

您无需在 xml 布局中定义广告视图,因为它已经在 MainActivity 类中以编程方式完成.您还可以实现 AdListener 来监听广告回调.

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.

相关文章