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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...