Advertisement
Guest User

MonoGame_AdMobCrash_Workaround_MainActivity.cs

a guest
Nov 25th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. using Android.App;
  2. using Android.Content;
  3. using Android.Gms.Ads;
  4. using Android.OS;
  5. using Android.Widget;
  6. using System;
  7.  
  8. namespace AdMobTest_MonoGame
  9. {
  10.     [Activity(Label = "MainActivity"
  11.         , MainLauncher = true
  12.         , LaunchMode = Android.Content.PM.LaunchMode.SingleInstance
  13.         )]
  14.     public class MainActivity : Activity
  15.     {
  16.         private bool _firstResume = true;
  17.  
  18.         protected override void OnCreate(Bundle savedInstanceState)
  19.         {
  20.             base.OnCreate(savedInstanceState);
  21.  
  22.             AdManager.Initialize(this);
  23.             AdManager.AdClosed += (sender, e) => ShowGameActivity();
  24.  
  25.             // Create your application here
  26.             LinearLayout layout = new LinearLayout(this);
  27.             layout.Orientation = Orientation.Vertical;
  28.  
  29.             layout.AddView(
  30.                 new TextView(this)
  31.                 {
  32.                     Text = "This is a temporary launcher to handle ad stuff... :)"
  33.                 }
  34.             );
  35.  
  36.             this.SetContentView(layout);
  37.         }
  38.  
  39.         protected override void OnResume()
  40.         {
  41.             base.OnResume();
  42.  
  43.             if (_firstResume)
  44.             {
  45.                 _firstResume = false;
  46.                 ShowGameActivity();
  47.             }
  48.             else
  49.             {
  50.                 AdManager.ShowAd();
  51.             }
  52.         }
  53.  
  54.         private void ShowGameActivity()
  55.         {
  56.             Intent showGameIntent = new Intent(this, typeof(Activity1));
  57.             this.StartActivity(showGameIntent);
  58.         }
  59.     }
  60.  
  61.     public static class AdManager
  62.     {
  63.         private static InterstitialAd _ad = null;
  64.  
  65.         public static bool AdReady { get; private set; } = false;
  66.         public static event EventHandler<EventArgs> AdClosed;
  67.  
  68.         public static void Initialize(Context context)
  69.         {
  70.             if (_ad != null)
  71.             {
  72.                 if (_ad.AdListener != null)
  73.                     _ad.AdListener.Dispose();
  74.                 _ad.Dispose();
  75.             }
  76.  
  77.             _ad = new InterstitialAd(context);
  78.             _ad.AdUnitId = "<< your ad unit id here >>";
  79.  
  80.             MyAdListener listener = new MyAdListener();
  81.             listener.AdLoaded += (sender, e) => AdReady = true;
  82.             listener.AdClosed +=
  83.                 (sender, e) =>
  84.                 {
  85.                     RequestAd();
  86.  
  87.                     if (AdClosed != null)
  88.                         AdClosed(null, e);
  89.                 };
  90.  
  91.             _ad.AdListener = listener;
  92.  
  93.             RequestAd();
  94.         }
  95.  
  96.         public static void ShowAd()
  97.         {
  98.             if (_ad.IsLoaded == true)
  99.                 _ad.Show();
  100.         }
  101.  
  102.         private static void RequestAd()
  103.         {
  104.             AdReady = false;
  105.             AdRequest.Builder builder = new AdRequest.Builder();
  106.             builder.AddTestDevice(AdRequest.DeviceIdEmulator);
  107.             AdRequest request = builder.Build();
  108.             _ad.LoadAd(request);
  109.         }
  110.     }
  111.  
  112.     public class MyAdListener : AdListener
  113.     {
  114.         public event EventHandler AdLoaded;
  115.         public event EventHandler AdClosed;
  116.  
  117.         public override void OnAdLoaded()
  118.         {
  119.             base.OnAdLoaded();
  120.  
  121.             if (this.AdLoaded != null)
  122.                 this.AdLoaded(this, new EventArgs());
  123.         }
  124.  
  125.         public override void OnAdClosed()
  126.         {
  127.             base.OnAdClosed();
  128.  
  129.             if (this.AdClosed != null)
  130.                 this.AdClosed(this, new EventArgs());
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement