2016-12-20 199 views

回答

1

对于Xamarin.Android,有两个SDK和分步示例Google AdMob。您将需要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(); 
       } 
      } 
     } 
    } 
} 

还有一个STE-一步的指导,让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); 
} 
+0

它会显示广告在所有页面中,还是仅引用MainActivity? –

+0

@ChandreshKhambhayata你需要自己完成代码。这是一个示例代码,它非常简单。它不适合盲文复制粘贴。 –

+0

当屏幕方向改变时,你有没有遇到过问题?目前在iOS上,只要设备从纵向翻转到横向或背面广告消失,我认为它会从屏幕上消失,或者尺寸变得混乱或者某种东西,但我无法弄清楚。试图使用智能横幅,顺便说一句。 – hvaughan3