Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Android.App;
- using Android.Content;
- using Android.Gms.Ads;
- using Android.OS;
- using Android.Widget;
- using System;
- namespace AdMobTest_MonoGame
- {
- [Activity(Label = "MainActivity"
- , MainLauncher = true
- , LaunchMode = Android.Content.PM.LaunchMode.SingleInstance
- )]
- public class MainActivity : Activity
- {
- private bool _firstResume = true;
- protected override void OnCreate(Bundle savedInstanceState)
- {
- base.OnCreate(savedInstanceState);
- AdManager.Initialize(this);
- AdManager.AdClosed += (sender, e) => ShowGameActivity();
- // Create your application here
- LinearLayout layout = new LinearLayout(this);
- layout.Orientation = Orientation.Vertical;
- layout.AddView(
- new TextView(this)
- {
- Text = "This is a temporary launcher to handle ad stuff... :)"
- }
- );
- this.SetContentView(layout);
- }
- protected override void OnResume()
- {
- base.OnResume();
- if (_firstResume)
- {
- _firstResume = false;
- ShowGameActivity();
- }
- else
- {
- AdManager.ShowAd();
- }
- }
- private void ShowGameActivity()
- {
- Intent showGameIntent = new Intent(this, typeof(Activity1));
- this.StartActivity(showGameIntent);
- }
- }
- public static class AdManager
- {
- private static InterstitialAd _ad = null;
- public static bool AdReady { get; private set; } = false;
- public static event EventHandler<EventArgs> AdClosed;
- public static void Initialize(Context context)
- {
- if (_ad != null)
- {
- if (_ad.AdListener != null)
- _ad.AdListener.Dispose();
- _ad.Dispose();
- }
- _ad = new InterstitialAd(context);
- _ad.AdUnitId = "<< your ad unit id here >>";
- MyAdListener listener = new MyAdListener();
- listener.AdLoaded += (sender, e) => AdReady = true;
- listener.AdClosed +=
- (sender, e) =>
- {
- RequestAd();
- if (AdClosed != null)
- AdClosed(null, e);
- };
- _ad.AdListener = listener;
- RequestAd();
- }
- public static void ShowAd()
- {
- if (_ad.IsLoaded == true)
- _ad.Show();
- }
- private static void RequestAd()
- {
- AdReady = false;
- AdRequest.Builder builder = new AdRequest.Builder();
- builder.AddTestDevice(AdRequest.DeviceIdEmulator);
- AdRequest request = builder.Build();
- _ad.LoadAd(request);
- }
- }
- public class MyAdListener : AdListener
- {
- public event EventHandler AdLoaded;
- public event EventHandler AdClosed;
- public override void OnAdLoaded()
- {
- base.OnAdLoaded();
- if (this.AdLoaded != null)
- this.AdLoaded(this, new EventArgs());
- }
- public override void OnAdClosed()
- {
- base.OnAdClosed();
- if (this.AdClosed != null)
- this.AdClosed(this, new EventArgs());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement