Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Android.App;
- using Android.Content.PM;
- using Android.Gms.Ads;
- using Android.OS;
- using Android.Views;
- using System;
- namespace AdMobTest
- {
- [Activity(Label = "AdMobTest"
- , MainLauncher = true
- , Icon = "@drawable/icon"
- , Theme = "@style/Theme.Splash"
- , AlwaysRetainTaskState = true
- , LaunchMode = Android.Content.PM.LaunchMode.SingleInstance
- , ScreenOrientation = ScreenOrientation.FullUser
- , ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize)]
- public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity, IAdProvider
- {
- private InterstitialAd _ad;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- CreateAndLoadAd();
- var g = new Game1(this);
- SetContentView((View)g.Services.GetService(typeof(View)));
- g.Run();
- }
- private void CreateAndLoadAd()
- {
- _ad = new InterstitialAd(this);
- // Obtain an ad unit id and replace the string below with it, then uncomment :)
- //string adUnitID = "<< YOUR AD UNIT ID >>";
- _ad.AdUnitId = adUnitID;
- if (_ad.AdListener != null)
- {
- _ad.AdListener.Dispose();
- _ad.AdListener = null;
- }
- var listener = new MyAdListener();
- listener.AdLoaded += (sender, e) =>
- {
- this.AdReady = true;
- System.Diagnostics.Debug.WriteLine("*** Ad loaded and ready.");
- };
- listener.AdClosed += (sender, e) => RequestAndLoadAd();
- _ad.AdListener = listener;
- RequestAndLoadAd();
- }
- private void RequestAndLoadAd()
- {
- this.AdReady = false;
- System.Diagnostics.Debug.WriteLine("*** Requesting and loading new ad.");
- var requestBuilder = new AdRequest.Builder().AddTestDevice(AdRequest.DeviceIdEmulator);
- var request = requestBuilder.Build();
- _ad.LoadAd(request);
- }
- public void ShowAd()
- {
- System.Diagnostics.Debug.WriteLine("*** ShowAd called.");
- if (_ad != null && _ad.IsLoaded == true)
- {
- _ad.Show();
- }
- }
- public bool AdReady { get; private set; } = false;
- }
- 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