Admob插页式广告不会展示

问题描述 投票:21回答:6

我曾经在我未来的应用上展示AdMob横幅广告,并且我想尝试一下插页式广告。

我检查了AdMob SDK的实现,我复制了他们的示例源代码,因为它正是我想要的(即活动启动时显示的插页式广告)。

我在模拟器和我的Galaxy上尝试过,没有显示任何广告。

这是源代码:

public class Asscreed extends Activity {
    private InterstitialAd interstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asscreed);

        // Create the interstitial.
        interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("ca-app-pub-6046034785851961/xxxxxx");

        // Create ad request.
        AdRequest adRequest = new AdRequest.Builder().build();

        // Begin loading your interstitial.
        interstitial.loadAd(adRequest);
    }

    // Invoke displayInterstitial() when you are ready to display an interstitial.
    public void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }
}

导入正常,Google Play服务库当然是导入的。

我用这个例子:AdMob Android Guides - Interstitial Ad

有人能告诉我我的代码有什么问题吗?

android admob interstitial
6个回答
45
投票

您应该等待广告加载。只有这样,您才可以调用displayInterstial()方法来显示广告。

您可以注册一个监听器,它会在加载完成时通知您。

interstitial.setAdListener(new AdListener(){
          public void onAdLoaded(){
               displayInterstitial();
          }
});

7
投票

这对我来说。

// Begin listening to interstitial & show ads.
interstitial.setAdListener(new AdListener(){
     public void onAdLoaded(){
          interstitial.show();
     }
});

我仍然想知道为什么谷歌的所有人上传代码实现方向,只有一个跟随他们到点后不工作..


6
投票

我有同样的问题。问题是,在清单中没有定义admob活动。请确保您的清单文件中包含以下活动标记

<activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />

2
投票
mInterstitialAd = new InterstitialAd(this);
                mInterstitialAd.setAdUnitId("Your Interstitial Id ca-app-pub-46563563567356235/3452455");
                AdRequest adRequest1 = new AdRequest.Builder()
                        .build();
                mInterstitialAd.loadAd(adRequest1);
                mInterstitialAd.setAdListener(new com.google.android.gms.ads.AdListener() {
                    @Override
                    public void onAdLoaded() {
                        mInterstitialAd.show();
                        super.onAdLoaded();

                    }
                });

1
投票

尝试在onCreate中初始化它,但只从事件中调用show()方法,例如单击按钮。

您可以使用此代码,测试ID工作:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    ...

        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); //test id

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); //test ad

        mInterstitialAd.loadAd(new AdRequest.Builder().build());


        btn_test.setOnClickListener(view -> {
            showInterstitial();
        });
...
    }    
    private void showInterstitial()
    {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
    }

-1
投票

你没有调用displayInsterstitial()。

但是你应该做的是为插页式广告调用听众:

interstitial.setAdListener(this);

然后它将实现您的Activity的Listener,然后在AdLoaded(或其他东西)上显示它。

© www.soinside.com 2019 - 2024. All rights reserved.