How to integrate AdMob ads in the latest MonoGame Android (XNA)?

前端 未结 1 521
别那么骄傲
别那么骄傲 2021-02-10 15:41

I have spent the last couple days researching AdMob integration for MonoGame Android and so far have not been able to successfully add a banner to the game I just made. All of t

相关标签:
1条回答
  • 2021-02-10 16:07

    After a long time looking for answers and testing multiple suggestions, I have finally found the solution.

    Xamarin has a Google Play Services component that can be downloaded from the component store. I had originally tried this, but was running into problems with my solution file in VS2012. So this is what I did to add the component manually:

    • Login and download the component from https://components.xamarin.com/view/googleplayservices/

    • Create a folder in your Android solution called "lib" and extract the dll files from the zipped component folder into it

    • Add the dll files as references to your project. You should now have these four references added:

      1. GooglePlayServicesLib
      2. Xamarin.Android.Support.v4
      3. Xamarin.Android.Support.v7.AppCompat
      4. Xamarin.Android.Support.v7.MediaRouter
    • Navigate into your project properties, click on the Application tab (first one) and under "Java Max Heap Size", type in 1G (otherwise you get a Java Heap out of memory error while compiling)

    • Make sure your App is set to use API 14 or higher (Android 4.0). This can be done is the project properties page as well

    • In your AndroidManifest.xml file, make sure you have the following:

      <activity 
          android:name="com.google.android.gms.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
      
    • Get your AdMob Unit ID

    • Change your Activity.cs file to something like this:

      using Android.Gms.Ads; // Add this include
      
      // Easy constants
      private const string AD_UNIT_ID = "YOUR_AD_ID";
      private const string TEST_DEVICE_ID = "YOUR_DEVICE_ID";
      private AdView adView;
      
      // Change OnCreate and make sure you're not trying to set SetContentView()
      // more than once
      protected override void OnCreate(Bundle bundle)
      {
          base.OnCreate(bundle);
          Puzzle.ARunningMan.Activity = this;
          var g = new Puzzle.ARunningMan();
      
          createAds(g.Window);
      
          g.Run();
      }
      
      // Wrapped everything in a function for less confusion.
      // Thanks to Dylan Wilson at Craftwork Games for the
      // simple layout
      private void createAds(AndroidGameWindow window)
      {
          var frameLayout = new FrameLayout(this);
          var linearLayout = new LinearLayout(this);
      
          linearLayout.Orientation = Orientation.Horizontal;
          linearLayout.SetGravity(Android.Views.GravityFlags.Right | Android.Views.GravityFlags.Bottom);
      
          frameLayout.AddView(window);
      
          adView = new AdView(this);
          adView.AdUnitId = AD_UNIT_ID;
          adView.AdSize = AdSize.Banner;
      
          linearLayout.AddView(adView);
          frameLayout.AddView(linearLayout);
          SetContentView(frameLayout);
      
          try
          {
              // Initiate a generic request.
              var adRequest = new AdRequest.Builder()
                  .AddTestDevice(AdRequest.DeviceIdEmulator)
                  .AddTestDevice(TEST_DEVICE_ID)
                  .Build();
      
              // Load the adView with the ad request.
              adView.LoadAd(adRequest);
          }
          catch (Exception ex)
          {
              // your error logging goes here
          }
      }
      
    • Compile (will take a little while at first), Run it, look in the LogCat window for a message tagged as "ads" that contains your device ID. More info here: http://webtutsdepot.com/2011/12/02/android-sdk-tutorial-get-admob-test-device-id/

    • Set the device ID string, recompile, run it

    And finally, if you wait about 30-60 seconds, you will see a test add show up as a banner. Good luck to all, I hope this helps, as it is now current information

    0 讨论(0)
提交回复
热议问题