如何在MainActivity中显示插页式广告?

时间:2018-09-27 15:00:03

标签: android admob interstitial

我希望每次用户访问MainActivity时都会显示一个插页式广告,但我不知道该如何进行。 谢谢!!!

我的代码:

   public class MainActivity extends AppCompatActivity {

    InterstitialAd mInterstitialAd;

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

        // FIREBASE INTERSTICIAL
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-2565065222479596/5036943452");

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {

                requestNewInterstitial();
            }
        });

        requestNewInterstitial();


    // FIREBASE INTERSTICIAL
    private void requestNewInterstitial() {
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
                .build();

        mInterstitialAd.loadAd(adRequest);
    }
}

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

问题在于您从未在插页式广告中致电.show()

根据documentation

  

插页式广告应在流程中的自然暂停期间展示   一个应用程序在游戏的关卡之间是一个很好的例子,或者   用户完成一项任务。要显示插页式广告,请使用isLoaded()   验证是否已完成加载的方法,然后调用show()。

我不建议每次用户访问活动时都显示它。 注意:您的代码将每次加载并显示另一个插页广告 一个插页广告完成加载-创建一个永无休止的插页广告循环!

public class MainActivity extends AppCompatActivity {

    InterstitialAd mInterstitialAd;

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

        // FIREBASE INTERSTITIAL
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-2565065222479596/5036943452");

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
                mInterstitialAd.show(); // add this line to show.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the interstitial ad is closed.
                requestNewInterstitial(); // never-ending loop of ads, avoid!!
            }
        });

        requestNewInterstitial();
    }

    // FIREBASE INTERSTITIAL
    private void requestNewInterstitial() {
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
                .build();

        mInterstitialAd.loadAd(adRequest);
    }
}