Admob非页内广告无法加载

时间:2016-03-09 09:07:06

标签: android admob

我正在尝试加载AdMob InterstitialAd添加时 1.应用程序打开
2.单击按钮
3.背部按下

问题
1.当应用程序打开时,其节目广告无法加载(在catlog中) 2.按下后退没有加载广告
3.第一次点击按钮时,广告加载但在第一次点击后它永远不会加载

public class MainActivity extends AppCompatActivity {

    InterstitialAd mInterstitialAd;
    Button mNewGameButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNewGameButton = (Button) findViewById(R.id.newgame_button);

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-5059726881726792/4446999064");
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            //Begin Game, continue with app
        }

        mNewGameButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    //Begin Game, continue with app
                }
            }
        });

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                //Begin Game, continue with app
            }
        });

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        // All emulators
            .addTestDevice("D016E92D0D3266640328381D0A8BB622")//bluestacks
            .addTestDevice("CE34804F0BB3F1F2F409E6DDFB272C60").build();//s3
        mAdView.loadAd(adRequest);
        mInterstitialAd.loadAd(adRequest);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mInterstitialAd = new InterstitialAd(MainActivity.this);
        mInterstitialAd.setAdUnitId("ca-app-pub-5059726881726792/4446999064");
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        // All emulators
            .addTestDevice("D016E92D0D3266640328381D0A8BB622")//bluestacks
            .addTestDevice("CE34804F0BB3F1F2F409E6DDFB272C60").build();//s3
        mAdView.loadAd(adRequest);
        mInterstitialAd.loadAd(adRequest);
    }
}

2 个答案:

答案 0 :(得分:2)

我在我的应用程序中使用此广告并正常工作。

看看下面的例子。

InterstitialAd mInterstitialAd;
    Button mNewGameButton;

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

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(<your ad unit id goes here>);
        final AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
        adRequestBuilder.addTestDevice("your test device id goes here");
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
            }
            @Override
            public void onAdClosed() {
                super.onAdClosed();
                mInterstitialAd.loadAd(adRequestBuilder.build());
            }
        });
        mInterstitialAd.loadAd(adRequestBuilder.build());

        mNewGameButton = (Button) findViewById(R.id.newgame_button);

        newgame_button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {

            case R.id.newgame_button:
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }

答案 1 :(得分:0)

  

加载总是花费很少的时间,希望您已经导入了SDK和清单声明(如果不是check it here)。这是详细说明

  1. 声明全局变量

    private lateinit var mInterstitialAd: InterstitialAd
    
  2. 在活动的onCreate中初始化广告

    mInterstitialAd = InterstitialAd(this)
    mInterstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712"
    mInterstitialAd.loadAd(AdRequest.Builder().build())
    
  3. 展示广告

           if (mInterstitialAd.isLoaded) {
                mInterstitialAd.show()
            } else {
                Log.d("isLoaded", "The interstitial wasn't loaded yet.")
            }
    
  4. adListener

    mInterstitialAd.adListener = object : AdListener() {
        override fun onAdClosed() {
    
            //go to second activity or do something
        }
    
        override fun onAdLoaded() {
            Log.d("zma interstitial", "loaded");
        }
    
        override fun onAdFailedToLoad(i: Int) {
            Log.w("AgeActivity", "onAdFailedToLoad:" + i)
        }
    }