xamarin.forms - 使用 Xamarin.Forms 的横幅广告

标签 xamarin.forms admob ads banner-ads

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

提前致谢。

最佳答案

Google AdMob 有 SDK 和分步示例适用于 Xamarin.Android。您将需要 Xamarin.GooglePlaySerives.Ads金块。

我用它在我在 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 ();
                }
            }
        }
    }
}

还有一个 AdMob 广告的分步指南 Xamarin.iOS :
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);
}

关于xamarin.forms - 使用 Xamarin.Forms 的横幅广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41247513/

相关文章:

javascript - 广告代码回传烦恼

c# - 如何在 Xamarin.Forms 中重用父 ContentView 中定义的样式

sqlite - Xamarin.Forms SQLite,在创建表时初始化数据

android - Admob 和发布应用程序?

ios - 广告 SDK 在没有应用程序 ID 的情况下初始化

android - 使用 MoPub 进行中介时看不到任何原生 AdMob 广告

android - 显示提示以了解位置

listview - 在 Xamarin 表单中将 ReactiveList<T> 绑定(bind)到 ListView

android - 在此适配器中添加一个 AdView

java - 类未找到异常 : Didn't find class "com.google.android.gms.ads.AdView"