【发布时间】:2019-02-23 17:25:27
【问题描述】:
我创建了一个应用并将其与 admob 连接。它工作了一个星期。然后..它刚刚在Android上停止了?我用我的基础应用程序 1:1 的实现创建了示例应用程序,以查看它是否与 admob 以外的其他东西相关?下面我放了我使用的 admob 的代码。
PCL 中的控制器:
public class AdMobView : View
{
public enum Sizes { Standardbanner, LargeBanner, MediumRectangle, FullBanner, Leaderboard, SmartBannerPortrait }
public Sizes Size { get; set; }
public AdMobView()
{
this.BackgroundColor = Color.Accent;
}
}
Android 渲染器
public class AdMobRenderer : ViewRenderer
{
Context context;
public AdMobRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged(e);
if(e.OldElement == null)
{
var adView = new AdView(Context);
switch ((Element as AdMobView).Size)
{
case AdMobView.Sizes.Standardbanner:
adView.AdSize = AdSize.Banner;
break;
case AdMobView.Sizes.LargeBanner:
adView.AdSize = AdSize.LargeBanner;
break;
case AdMobView.Sizes.MediumRectangle:
adView.AdSize = AdSize.MediumRectangle;
break;
case AdMobView.Sizes.FullBanner:
adView.AdSize = AdSize.FullBanner;
break;
case AdMobView.Sizes.Leaderboard:
adView.AdSize = AdSize.Leaderboard;
break;
case AdMobView.Sizes.SmartBannerPortrait:
adView.AdSize = AdSize.SmartBanner;
break;
default:
adView.AdSize = AdSize.Banner;
break;
}
adView.AdUnitId = "ca-app-pub-3940256099942544/6300978111";
var testDeviceId = Android.Provider.Settings.Secure.GetString(Context.ContentResolver, Android.Provider.Settings.Secure.AndroidId);
var requestBuilder = new AdRequest.Builder().AddTestDevice("c793762db79ab2c6");
Console.WriteLine("Android device id is: {0}", testDeviceId);
adView.LoadAd(requestBuilder.Build());
SetNativeControl(adView);
}
}
}
MainActivity Andorid
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
var id = "my-app-id-with-~";
MobileAds.Initialize(ApplicationContext, id);
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
}
主页
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:AdMobFixed"
xmlns:adMob="Ad"
x:Class="AdMobFixed.MainPage">
<StackLayout>
<!-- Place new controls here -->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label
Grid.Row="0"
Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<local:AdMobView
MinimumWidthRequest="400"
MinimumHeightRequest="100"
HorizontalOptions="CenterAndExpand"
VerticalOptions="EndAndExpand"/>
</Grid>
</StackLayout>
一周前我不必使用最小请求。不幸的是,我用安卓操作系统弄坏了我的手机,所以我无法在我的电脑上测试它。下面我附上了我的输出:
02-23 17:14:47.015 D/DynamitePackage(19883):实例化 com.google.android.gms.ads.ChimeraAdManagerCreatorImpl
02-23 17:14:47.025 I/Ads (19883):此请求是从测试设备发送的。
02-23 17:14:47.096 I/DynamiteModule(19883):考虑本地模块 com.google.android.gms.ads.dynamite:0 和远程模块 com.google.android.gms.ads.dynamite:11140
02-23 17:14:47.097 I/DynamiteModule(19883):com.google.android.gms.ads.dynamite 的选定远程版本,版本 >= 11140
Android Manifest 也会更新互联网权限和网络状态。我还在应用标签之间添加了活动标签。
我使用的是谷歌的 nuget Ads lite,我确实在谷歌的完整 play.ads 上对其进行了测试。
如有任何帮助,我将非常高兴。先感谢您 ! :)
【问题讨论】:
-
我认为您的代码没有任何问题。您可以在 Google 开发者论坛上发布您的问题。
标签: c# android xamarin xamarin.android admob