29 Mayıs 2015 Cuma

Xamarin Android Admob Banners and InterstitialAds

Here is the sample for Admob elements in xamarin android apps.

First, you need Google Play Services

https://components.xamarin.com/view/googleplayservices/

AndroidManifest.xml

<application>....
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
.....
</application>
   Also add INTERNET and ACCESS_NETWORK_STATE permissions.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

Monogame Activity

[Activity(Label = "Sample Admob App"
          , MainLauncher = true
          , Icon = "@drawable/ic_launcher"
          , Theme = "@style/Theme.Splash"
          , AlwaysRetainTaskState = true
          , LaunchMode = Android.Content.PM.LaunchMode.SingleTop
          , ScreenOrientation = ScreenOrientation.Portrait
          , ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize)]
    public class TestActivity : Microsoft.Xna.Framework.AndroidGameActivity
    {
        private AdView ad;    
        private static InterstitialAd interstitial;
        private string addid = "YOUR ADMOB ID";
        private LinearLayout ll;
        private FrameLayout fl;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            var g = new Engine.GameEngine();
            //Game engine activity variable
            g.activity = this;          
            //Create interstital ad view
            interstitial = AdWrapper.ConstructFullPageAdd(this, addid);
            //Create FrameLayout
            fl = new FrameLayout(this);
            fl.AddView((View)g.Services.GetService(typeof(View)));
            SetContentView(fl);
            g.Run();
         
        }

        public void ShowInterstitialAd()
        {
            RunOnUiThread(() =>
            {
                if (interstitial.AdListener != null)
                    interstitial.AdListener.Dispose();
                interstitial.AdListener = null;
                var intlistener = new AdMob.adlistener();
                intlistener.AdLoaded += () => { if (interstitial.IsLoaded)interstitial.Show(); };
                interstitial.AdListener = intlistener;

                interstitial.CustomBuild();
            });
        }

        public void HideBannerAd()
        {
            if (ll != null)
            {
                fl.RemoveView(ll);
                ll.RemoveView(ad);
                ad.Dispose();
                ad = null;
                ll.Dispose();
                ll = null;
            }
        }
        public void RefreshBannerAd()
        {      
            if (ll == null)
            {
                HideBannerAd();
                ll = new LinearLayout(this);
                ll.Orientation = Orientation.Horizontal;          
                ll.SetGravity(GravityFlags.Bottom | GravityFlags.Center);

                ad = new AdView(this);
                ad.AdSize = AdSize.Banner;
                ad.AdUnitId = addid;
                ll.AddView(ad);
                fl.AddView(ll);
            }
            using (var requestbuilder = new AdRequest.Builder())
            {
                ad.LoadAd(requestbuilder.Build());
            }
        }

    }


AdWrapper


namespace AdMob
{

    public static class AdWrapper
    {
        public static InterstitialAd ConstructFullPageAdd(Context con, string UnitID)
        {
            var ad = new InterstitialAd(con);
            ad.AdUnitId = UnitID;
            return ad;
        }
        public static AdView ConstructStandardBanner(Context con, AdSize adsize, string UnitID)
        {
            var ad = new AdView(con);
            ad.AdSize = adsize;
            ad.AdUnitId = UnitID;
            return ad;
        }

        public static InterstitialAd CustomBuild(this InterstitialAd ad)
        {
         
            var requestbuilder = new AdRequest.Builder();
       
            ad.LoadAd(requestbuilder.Build());
            return ad;
        }
        public static AdView CustomBuild(this AdView ad)
        {
            var requestbuilder = new AdRequest.Builder();
            ad.LoadAd(requestbuilder.Build());
            return ad;
        }
    }
}


AdListener

namespace AdMob
{
    class adlistener : AdListener
    {
        // Declare the delegate (if using non-generic pattern).
        public delegate void AdLoadedEvent();
        public delegate void AdClosedEvent();
        public delegate void AdOpenedEvent();



        // Declare the event.
        public event AdLoadedEvent AdLoaded;
        public event AdClosedEvent AdClosed;
        public event AdOpenedEvent AdOpened;
        public event AdClosedEvent AdFailed;
        public override void OnAdFailedToLoad(int p0)
        {
            if (AdFailed != null) this.AdFailed();
            base.OnAdFailedToLoad(p0);
        }
        public override void OnAdLoaded()
        {
            if (AdLoaded != null) this.AdLoaded();
            base.OnAdLoaded();
        }

        public override void OnAdClosed()
        {
            if (AdClosed != null) this.AdClosed();
            base.OnAdClosed();
        }
        public override void OnAdOpened()
        {
            if (AdOpened != null) this.AdOpened();
            base.OnAdOpened();
        }
    }
}

Hiç yorum yok:

Yorum Gönder