在Android游戏的Android表面视图上集成AdMob横幅广告

时间:2018-06-19 01:22:16

标签: java android admob

我正在开发一款安卓游戏。上下文视图扩展了表面视图类。我试图在屏幕底部放一个横幅。我使用this guide设置了Google AdMob SDK。我已将AdView添加到布局中,并使用another guide加载了测试广告。

这是我的MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new GameView(this));

        MobileAds.initialize(this, "ca-app-pub-1042328490971088~5397056328");

        AdView adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
    }
}

当我运行此代码时,在我的游戏继续正常运行时,根本不会显示任何广告。我的想法是广告可能存在,但是因为游戏视图占据整个屏幕而不可见。如果有人可以帮我确保我的横幅广告重叠在画布上或画布缩小以容纳广告,我将不胜感激。

这里还有我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.haakamaujla.myfirstgame">

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="Stunty Skys"
        android:roundIcon="@drawable/icon"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

搜索帖子后,我找到了问题的答案:

adView.setBackgroundColor(Color.TRANSPARENT);

显然,这会迫使广告展示,否则就不会一直展示。

答案 1 :(得分:0)

您缺少横幅广告的布局。 试试这个,它会完美地工作:

    MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111");

    RelativeLayout layout = new RelativeLayout(this);
    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
            );
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

    // Create a banner ad
    AdView mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

    // Create an ad request.
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

    // Optionally populate the ad request builder.
    adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

    View gameView = (new GameView(this));

    layout.addView(gameView);
    layout.addView(mAdView, adParams);

    // Start loading the ad.
    mAdView.loadAd(adRequestBuilder.build());

    setContentView(layout);


    mAdView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            Toast.makeText(Main2Activity.this, "Enjoy", Toast.LENGTH_SHORT).show();
            // Code to be executed when an ad finishes loading.
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            // Code to be executed when an ad request fails.
            Toast.makeText(Main2Activity.this, "Sorry bro", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdOpened() {
            // Code to be executed when an ad opens an overlay that
            // covers the screen.
        }

        @Override
        public void onAdLeftApplication() {
            // Code to be executed when the user has left the app.
        }

        @Override
        public void onAdClosed() {
            // Code to be executed when when the user is about to return
            // to the app after tapping on an ad.
        }
    });