将 Admob 添加到 libgdx

Adding Admob to libgdx(将 Admob 添加到 libgdx)
本文介绍了将 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"

Nothing is shown, no ad, why, I added also in build.gradle (Project:projectN) compile "com.google.android.gms:play-services-ads:$admobVersion"

使用 ubuntu 16.04,android-studio

Using ubuntu 16.04, android-studio

推荐答案

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

Add AdMob Ads without Firebase :

  1. 将这些行放在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" />
    

    <application标签内添加Activity如果要使用Interstitial Ads

    Inside <application tag add Activity if want to use Interstitial Ads

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

  • AndroidLauncher class.

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

  • 这篇关于将 Admob 添加到 libgdx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

    相关文档推荐

    Show padding zeros using DecimalFormat(使用 DecimalFormat 显示填充零)
    Java decimal formatting using String.format?(使用 String.format 的 Java 十进制格式?)
    Sprintf equivalent in Java(Java 中的 Sprintf 等价物)
    StAX XML formatting in Java(Java 中的 StAX XML 格式)
    Java - format double value as dollar amount(Java - 将双值格式化为美元金额)
    Java printf using variable field size?(Java printf使用可变字段大小?)