在没有按下按钮的情况下添加插页式广告adob广告

时间:2017-02-23 17:45:13

标签: android admob interstitial

我想通过admob添加插页式广告,而不按任何按钮,但是 它似乎没有帮助我尝试下面的代码:

...
public class MainActivity extends ActionBarActivity {

    InterstitialAd mInterstitialAd;
    Button mNewGameButton;

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


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

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                requestNewInterstitial();
                beginPlayingGame();
            }
        });

        requestNewInterstitial();

        mNewGameButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    beginPlayingGame();
                }
            }
        });

        beginPlayingGame();
    }

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

        mInterstitialAd.loadAd(adRequest);
    }

    private void beginPlayingGame() {
        // Play for a while, then display the New Game Button
    }
}
...

2 个答案:

答案 0 :(得分:2)

加载添加时,您必须添加一个用于测试的设备ID。当你运行这段代码时,请查看你的logcat以查看设备ID。将该设备ID复制并粘贴到添加测试设备中并重新运行你将开始看到添加

公共类MainActivity扩展了ActionBarActivity {

InterstitialAd mInterstitialAd;
Button mNewGameButton;

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


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

AdRequest adRequest = new AdRequest.Builder()                 .addTestDevice( “SEE_YOUR_LOGCAT_TO_GET_YOUR_DEViCE_ID”)建立();

    mInterstitialAd.loadAd(adRequest);
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();
            beginPlayingGame();
        }
    });

    requestNewInterstitial();

            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            } else {
                beginPlayingGame();
            }


    beginPlayingGame();
}

答案 1 :(得分:0)

我在这里找到了真正的答案

interstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            MainActivity.this.interstitialAd.show();
        }
    }, 5000);
}}  );