gpt4 book ai didi

java - 将 Admob 添加到 libgdx

转载 作者:行者123 更新时间:2023-12-02 13:09:14 24 4
gpt4 key购买 nike

    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 中添加了(项目:projectN)编译“com.google.android.gms:play-services-ads:$admobVersion”

使用ubuntu 16.04,android-studio

最佳答案

在不使用 Firebase 的情况下添加 AdMob 广告:

  1. 将这些行放入 build.gradle 内android 模块。

    dependencies {

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

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

    内部<application标签添加Activity如果想使用插页式广告

    <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 类。

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

关于java - 将 Admob 添加到 libgdx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44014374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com