Advertisement
Guest User

MonoGame_AdMobCrash_Activity1.cs

a guest
Nov 22nd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. using Android.App;
  2. using Android.Content.PM;
  3. using Android.Gms.Ads;
  4. using Android.OS;
  5. using Android.Views;
  6. using System;
  7.  
  8. namespace AdMobTest
  9. {
  10.     [Activity(Label = "AdMobTest"
  11.         , MainLauncher = true
  12.         , Icon = "@drawable/icon"
  13.         , Theme = "@style/Theme.Splash"
  14.         , AlwaysRetainTaskState = true
  15.         , LaunchMode = Android.Content.PM.LaunchMode.SingleInstance
  16.         , ScreenOrientation = ScreenOrientation.FullUser
  17.         , ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize)]
  18.     public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity, IAdProvider
  19.     {
  20.         private InterstitialAd _ad;
  21.  
  22.         protected override void OnCreate(Bundle bundle)
  23.         {
  24.             base.OnCreate(bundle);
  25.  
  26.             CreateAndLoadAd();
  27.  
  28.             var g = new Game1(this);
  29.             SetContentView((View)g.Services.GetService(typeof(View)));
  30.             g.Run();
  31.         }
  32.  
  33.         private void CreateAndLoadAd()
  34.         {
  35.             _ad = new InterstitialAd(this);
  36.             // Obtain an ad unit id and replace the string below with it, then uncomment :)
  37.             //string adUnitID = "<< YOUR AD UNIT ID >>";
  38.             _ad.AdUnitId = adUnitID;
  39.  
  40.             if (_ad.AdListener != null)
  41.             {
  42.                 _ad.AdListener.Dispose();
  43.                 _ad.AdListener = null;
  44.             }
  45.  
  46.             var listener = new MyAdListener();
  47.             listener.AdLoaded += (sender, e) =>
  48.             {
  49.                 this.AdReady = true;
  50.                 System.Diagnostics.Debug.WriteLine("*** Ad loaded and ready.");
  51.             };
  52.  
  53.             listener.AdClosed += (sender, e) => RequestAndLoadAd();
  54.             _ad.AdListener = listener;
  55.  
  56.             RequestAndLoadAd();
  57.         }
  58.  
  59.         private void RequestAndLoadAd()
  60.         {
  61.             this.AdReady = false;
  62.             System.Diagnostics.Debug.WriteLine("*** Requesting and loading new ad.");
  63.             var requestBuilder = new AdRequest.Builder().AddTestDevice(AdRequest.DeviceIdEmulator);
  64.             var request = requestBuilder.Build();
  65.  
  66.             _ad.LoadAd(request);
  67.         }
  68.  
  69.         public void ShowAd()
  70.         {
  71.             System.Diagnostics.Debug.WriteLine("*** ShowAd called.");
  72.             if (_ad != null && _ad.IsLoaded == true)
  73.             {
  74.                 _ad.Show();
  75.             }
  76.         }
  77.  
  78.         public bool AdReady { get; private set; } = false;
  79.     }
  80.  
  81.     public class MyAdListener : AdListener
  82.     {
  83.         public event EventHandler AdLoaded;
  84.         public event EventHandler AdClosed;
  85.  
  86.         public override void OnAdLoaded()
  87.         {
  88.             base.OnAdLoaded();
  89.  
  90.             if (this.AdLoaded != null)
  91.                 this.AdLoaded(this, new EventArgs());
  92.         }
  93.  
  94.         public override void OnAdClosed()
  95.         {
  96.             base.OnAdClosed();
  97.  
  98.             if (this.AdClosed != null)
  99.                 this.AdClosed(this, new EventArgs());
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement