How to put InterstitialAD in the application?

-1

How do I place that ad that occupies the entire screen in my application, and is only closed when the person clicks the X?

    
asked by anonymous 19.04.2017 / 01:49

1 answer

1

Full-screen ads, called Interstitial Ads covers the entire application screen, such as < in> FullScreen . See below as directed and described in the own documentation :

First you must instantiate an InterstitialAd object

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

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

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

See the complete code for what your main should look like:

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
    }
}
    
19.04.2017 / 13:56