我创建了一个应用并将其与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 adrenderer
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>
一周前,我不必使用最小请求。不幸的是,我用Android操作系统摔坏了手机,因此无法在PC上进行测试。下面我附上我的输出:
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也已更新为具有Internet权限和网络状态。我还在应用标记之间添加了活动标记。
我使用的是Google的nuget Ads lite表单,并且在Google的full play.ads上进行了测试。
对于任何帮助,我都会很高兴。先感谢您 ! :)
答案 0 :(得分:0)
尝试添加以下代码:MobileAds.Initialize(ApplicationContext,id); 在下面的行上: base.OnCreate(savedInstanceState);