How to put Interstitialad in the app?

Asked

Viewed 25 times

-1

How do I put that ad that occupies the entire screen in my app, and it only closes when the person clicks on X?

1 answer

1


Full screen ads, called Interstitial ads covers the entire application screen, as a mode Fullscreen. See below as indicated and described in own documentation:

First an Interstitial object must be instantiated

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

In button action you should check your ad was loaded using isLoad(). See:

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

See the full code of how your main:

public class MainActivity extends ActionBarActivity {

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

        // aqui o seu adUnitId 
        mInterstitialAd.setAdUnitId("seu adUnitID aqui");

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.