Link to off. documentation: https://developers.google.com/admob/android/interstitial

I did everything described there - it works, but only at the touch of a button. And how to do it so that every time you launch the application, ads are displayed 1 time?

  • Tags (tags) are needed to highlight the main points of the QUESTION, according to which other users will be able to find a solution to a similar problem more quickly, and not to demonstrate their own preferences in choosing an IDE. The question has nothing to do with problems when working with IDE Android Studio and this tag is not needed in the question. - pavlofff

2 answers 2

  1. It is necessary to track that the user opened the application, and not (for example) turned. This can be done by checking if(savedInstanceState==null) is an argument in the onCreate method. After its rotation (re-creation), this argument is no longer null and the condition will not be fulfilled.
  2. In this condition, create a request to AdMob
  3. Launch it and wait until the advertisement is displayed.

     public class MainActivity extends ActionBarActivity { boolean adsAlredyShown = false; InterstitialAd mInterstitialAd; @Override protected void onCreate(Bundle savedInstanceState) { Log.i("LOG", "onCreate"); super.onCreate(savedInstanceState); mInterstitialAd = new InterstitialAd(this); mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); //идентификатор из доков надо заменить на свой mInterstitialAd.setAdListener(new AdListener() { @Override public void onAdClosed() { Log.i("LOG", "onAdClosed"); } public void onAdLeftApplication() { Log.i("LOG", "onAdLeftApplication"); } @Override public void onAdLoaded() { Log.i("LOG", "onAdLoaded"); mInterstitialAd.show(); } public void onAdFailedToLoad(int errorCode) { Log.e("LOG", "onAdFailedToLoad with errorCode " + errorCode); } @Override public void onAdOpened() { Log.i("LOG", "onAdOpened"); //вызывается в момент отображения рекламы. //и раз она отобразилась ставим флаг в true //чтобы больше её не показывать adsAlredyShown = true; } }); if(savedInstanceState==null) { AdRequest adRequest = new AdRequest.Builder().build(); mInterstitialAd.loadAd(adRequest); } else { adsAlreadyShown = savedInstanceState.getBoolean("AdsAlreadyShown", false); //реклама ещё не показывалась if(!adsAlreadyShown) { AdRequest adRequest = new AdRequest.Builder().build(); mInterstitialAd.loadAd(adRequest); } } } protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean("AdsAlreadyShown", adsAlreadyShown); } } 

Also, in the same savedInstanceState you can put a flag after the advertisement is shown and not show it while it is true . This flag will disappear only if the activation is killed by the system or manually using the finish() method.

  • Complete the plis code about the flag (I don’t understand how to place the flag)? - Andrei
  • @ Andrey, you don’t have to do this. And without this should work. - YurySPb
  • @Andrey, and so the flag is just a boolean written in the Bundle in the onSaveInstanceState method and retrieved in onCreate from the argument. All this is easy to google. And you need to put it in the ad listener in the method that is called after it is shown / followed by the user in the link in it. All this is in the docks of AdMob-a - YuriySPb
  • one
    Thank you very much YOU ALWAYS HELP! RESPECT)) - Andrew
  • @Andrey, please) Just keep in mind that if you rotate the screen before displaying an advertisement, it will not seem more until a full restart. This can be solved in the second way. But today I will not add - maybe tomorrow. But it's easy to google. - JuriySPb

So, according to Google's rules, is it forbidden to display ads at the start of the application?

Invalid interstitial ad placement methods

  • Immediately after the launch is impossible, but, for example, after SplashScreen you can. Because the user will already know that this application has started. - web_alex