问题描述
过去几天我一直在研究 MonoGame Android 的 AdMob 集成,但到目前为止还未能成功地为我刚刚制作的游戏添加横幅.到目前为止,我发现的所有答案都非常过时,而且我发现的所有示例都不能在最新的 Android API 中运行.
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 the answers I have found so far are terribly outdated and none of the examples I found are working in the latest Android APIs.
我在 Visual Studio 中使用 MonoGame 3.2 的开发版本 #983.
I am using development build #983 of MonoGame 3.2 in Visual Studio.
我试过了:
使用此 github 存储库中的示例:/CartBlanche/MonoGame-Samples/tree/master/AdMob
using the sample found in this github repo: /CartBlanche/MonoGame-Samples/tree/master/AdMob
以及在此 github 存储库中找到的示例:/xamarin/monodroid-samples/tree/master/AdMob
as well as the sample found in this github repo: /xamarin/monodroid-samples/tree/master/AdMob
更新 SDK 管理器以下载 google play services extras
Updating the SDK manager to download the google play services extras
遵循此页面上的代码示例:http://www.craftworkgames.com/blog/monogame-code-snippets/using-admob-with-monogame/
Following the code sample at this page: http://www.craftworkgames.com/blog/monogame-code-snippets/using-admob-with-monogame/
以及在互联网上找到的其他方法.在将 JAVA 源和 JAR 文件添加到项目中时,我一直非常小心地选择正确的构建选项,但我从未找到任何 AdView 类,并且不再存在的Google Mobile Ads SDK v6.4.1"JAR由谷歌支持,如下所述:https://developers.google.com/mobile-ads-sdk/
As well as other methods found all over the internet. I have been very careful to select the correct build options when adding JAVA sources and JAR files to the project, but I have never found any AdView class, and the "Google Mobile Ads SDK v6.4.1" JAR that is out there is no longer supported by google, as outlined here: https://developers.google.com/mobile-ads-sdk/
如果有人有任何新的和最新的方法将 AdMob 广告集成到 MonoGame Android 项目中,我认为答案需要刷新(我将非常感激)=)
If anyone has any new and up-to-date methods for integrating AdMob ads into a MonoGame Android project, I think the answers need a refresh (and I will be very grateful) =)
推荐答案
找了好久,测试了多条建议,终于找到了解决方案.
After a long time looking for answers and testing multiple suggestions, I have finally found the solution.
Xamarin 有一个可以从组件商店下载的 Google Play 服务组件.我最初尝试过这个,但是在 VS2012 中遇到了我的解决方案文件的问题.所以这就是我手动添加组件所做的:
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:
从 https://components.xamarin.com/view/googleplayservices/
在您的 Android 解决方案中创建一个名为lib"的文件夹,并将压缩后的组件文件夹中的 dll 文件提取到其中
Create a folder in your Android solution called "lib" and extract the dll files from the zipped component folder into it
将 dll 文件添加为对项目的引用.您现在应该添加了这四个引用:
Add the dll files as references to your project. You should now have these four references added:
- GooglePlayServicesLib
- Xamarin.Android.Support.v4
- Xamarin.Android.Support.v7.AppCompat
- Xamarin.Android.Support.v7.MediaRouter
导航到您的项目属性,单击应用程序"选项卡(第一个)并在Java Max Heap Size"下输入 1G(否则编译时会出现 Java Heap out of memory 错误)
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)
确保您的应用设置为使用 API 14 或更高版本 (Android 4.0).项目属性页也可以这样做
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
在您的 AndroidManifest.xml 文件中,确保您具有以下内容:
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" />
获取您的 AdMob 单元 ID
Get your AdMob Unit ID
将您的 Activity.cs 文件更改为如下内容:
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
}
}
编译(一开始需要一点时间),运行它,在 LogCat 窗口中查看标记为广告"的消息,其中包含您的设备 ID.更多信息:http://webtutsdepot.com/2011/12/02/android-sdk-tutorial-get-admob-test-device-id/
设置设备ID字符串,重新编译,运行
Set the device ID string, recompile, run it
最后,如果您等待大约 30-60 秒,您会看到一个测试添加显示为横幅.祝大家好运,我希望这会有所帮助,因为它现在是最新信息
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
这篇关于如何在最新的 MonoGame Android (XNA) 中集成 AdMob 广告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!