横幅广告与Xamarin.Forms

时间:2016-12-20 16:57:40

标签: xamarin.forms admob advertising advertisement banner-ads

我只想了解Xamarin.Forms支持的横幅广告,没有任何补丁或漏洞。是否有任何广告提供商为他们的SDK提供Xamarin.Forms?

提前致谢。

1 个答案:

答案 0 :(得分:1)

对于Xamarin.Android,Google AdMob有SDK和分步示例。您将需要Xamarin.GooglePlaySerives.Ads nuget。

我用它在我在Google Play上发布的Xamarin.Forms应用中展示广告。

以下是应用程序的android部分的示例代码:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Android.Gms.Ads;
using Android;

namespace AdMobExample
{
    [Activity (Label = "@string/app_name", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        protected AdView mAdView;
        protected InterstitialAd mInterstitialAd;
        protected Button mLoadInterstitialButton;

        protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);
            SetContentView (Resource.Layout.activity_main);

            mAdView = FindViewById<AdView> (Resource.Id.adView);
            var adRequest = new AdRequest.Builder ().Build ();
            mAdView.LoadAd (adRequest);

            mInterstitialAd = new InterstitialAd (this);
            mInterstitialAd.AdUnitId = GetString (Resource.String.test_interstitial_ad_unit_id);

            mInterstitialAd.AdListener = new AdListener (this);

            mLoadInterstitialButton = FindViewById<Button> (Resource.Id.load_interstitial_button);
            mLoadInterstitialButton.SetOnClickListener (new OnClickListener (this));
        }

        protected void RequestNewInterstitial ()
        {
            var adRequest = new AdRequest.Builder ().Build ();
            mInterstitialAd.LoadAd (adRequest);
        }

        protected void BeginSecondActivity ()
        {
            var intent = new Intent (this, typeof(SecondActivity));
            StartActivity (intent);
        }

        protected override void OnPause ()
        {
            if (mAdView != null) {
                mAdView.Pause ();
            }
            base.OnPause ();
        }

        protected override void OnResume ()
        {
            base.OnResume ();
            if (mAdView != null) {
                mAdView.Resume ();
            }
            if (!mInterstitialAd.IsLoaded) {
                RequestNewInterstitial ();
            }
        }

        protected override void OnDestroy ()
        {
            if (mAdView != null) {
                mAdView.Destroy ();
            }
            base.OnDestroy ();
        }

        class AdListener : Android.Gms.Ads.AdListener
        {
            MainActivity that;

            public AdListener (MainActivity t)
            {
                that = t;
            }

            public override void OnAdClosed ()
            {
                that.RequestNewInterstitial ();
                that.BeginSecondActivity ();
            }
        }

        class OnClickListener : Java.Lang.Object, View.IOnClickListener
        {
            MainActivity that;

            public OnClickListener (MainActivity t)
            {
                that = t;
            }

            public void OnClick (View v)
            {
                if (that.mInterstitialAd.IsLoaded) {
                    that.mInterstitialAd.Show ();
                } else {
                    that.BeginSecondActivity ();
                }
            }
        }
    }
}

还为Xamarin.iOS的AdMob广告提供了分步指南:

using Google.MobileAds;
...

const string intersitialId = "<Get your ID at google.com/ads/admob>";

Interstitial adInterstitial;

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();

    CreateAndRequestInterstitial ();
}

public void AfterSomeTime ()
{   
    if (adInterstitial.IsReady)
        adInterstitial.PresentFromRootViewController (navController);
}

void CreateAndRequestInterstitial ()
{
    adInterstitial = new Interstitial (intersitialId);
    adInterstitial.ScreenDismissed += (sender, e) => {
        // Interstitial is a one time use object. That means once an interstitial is shown, HasBeenUsed 
        // returns true and the interstitial can't be used to load another ad. 
        // To request another interstitial, you'll need to create a new Interstitial object.
        adInterstitial.Dispose ();
        adInterstitial = null;
        CreateAndRequestInterstitial ();
    };

    var request = Request.GetDefaultRequest ();
    // Requests test ads on devices you specify. Your test device ID is printed to the console when
    // an ad request is made. GADBannerView automatically returns test ads when running on a
    // simulator. After you get your device ID, add it here
    request.TestDevices = new [] { Request.SimulatorId.ToString () };

    adInterstitial.LoadRequest (request);
}