понеделник, февруари 10, 2014

Android - display AdMob interstitials on app exit

public class MyActivity extends Activity 
{ 
   private static final String MY_AD_UNIT_ID = "...";
   private InterstitialAd interstitial;
 
   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState);
      setupAds();
   }
 
   @Override
   public void onBackPressed() 
   {
      super.onBackPressed();
      if(interstitial.isReady()) {
         interstitial.show();
      } else {
         MyActivity.this.finish();
      }
   }
  
   private void setupAds() 
   {
      interstitial = new InterstitialAd(this, MY_AD_UNIT_ID);
      interstitial.setAdListener(new AdListener() 
      {
         public void onReceiveAd(Ad ad) {}
         public void onPresentScreen(Ad ad) {}
         public void onLeaveApplication(Ad ad) {
            MyActivity.this.finish();
         }
         public void onFailedToReceiveAd(Ad ad, ErrorCode errorCode) {}
         public void onDismissScreen(Ad ad) {
            MyActivity.this.finish();
         } 
      });
      AdRequest adRequest = new AdRequest();
      interstitial.loadAd(adRequest);
   }
}

Няма коментари: