2016-12-20 5 views

Antwort

1

Es gibt sowohl SDK als auch Schritt-für-Schritt-Beispiele für Google AdMob für Xamarin.Android. Sie werden die Xamarin.GooglePlaySerives.Ads nugget benötigen.

Ich verwende es, um Anzeigen in meiner Xamarin.Forms App zu zeigen, die bei Google Play veröffentlicht wurde.

Hier ist der Beispielcode für den Android-Teil Ihrer Anwendung:

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(); 
       } 
      } 
     } 
    } 
} 

Es gibt auch eine ste-für-Schritt-Anleitung für AdMob-Anzeigen für 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

Wird es Anzeigen zu schalten auf allen Seiten oder auf MainActivity Only bezogen? –

+0

@ChandreshKhambhayata Sie müssen den Code selbst durchlesen. Es ist ein Beispielcode und es ist sehr einfach. Es ist nicht für Blindkopie geeignet. –

+0

Hatten Sie jemals Probleme, wenn die Bildschirmausrichtung geändert wird? Bei iOS wird die Anzeige jedes Mal, wenn das Gerät vom Hochformat ins Querformat oder umgekehrt gedreht wird, ausgeblendet. Ich gehe davon aus, dass es nicht mehr auf dem Bildschirm angezeigt wird oder dass die Größe durcheinander gebracht wird, aber ich kann es nicht herausfinden. Versuche, Smart Banners zu verwenden, btw. – hvaughan3