微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 将Admob添加到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);

没有显示任何内容,没有广告,为什么,
我也在build.gradle中添加了(Project:projectN)
编译“com.google.android.gms:play-services-ads:$admobVersion”

使用ubuntu 16.04,android-studio

解决方法:

添加没有Firebase的AdMob广告:

>将这些行放在android模块的build.gradle中.

dependencies {

    compile 'com.google.android.gms:play-services-ads:10.2.4'
}

>在AndoidManifest.xml文件添加权限

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

如果要使用非页内广告,请在里面<应用程序标记添加活动

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐