简体   繁体   中英

Add an interstitial admob ads without button pressed

I want to add an interstitial ads by admob without press any button but
it does not appear can you help I tried below codes:

...
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
    }
}
...

While loading an add you must add one device id that you are using for testing . while ur running this code see ur logcat to see the device id .. Copy and paste that device id into add test device and rerun u will start seeing add

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");

AdRequest adRequest = new AdRequest.Builder() .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEViCE_ID").build();

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

    requestNewInterstitial();

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


    beginPlayingGame();
}

the true answer here i found it

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM