Hello,
please, tell me how to use AdMob on MG 3.6?
I try to use admob but I dont see any ads. What I do wrong?
public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
{
private AdView bannerAd;
public AdView BannerAd
{
get { return bannerAd; }
}
private static InterstitialAd interstitial;
private const string bannerID = “ca-app-pub-59”;
private const string interstitialID = “ca-app-pub-59”;
private const string testDeviceID = “7772”;
private LinearLayout ll;
private FrameLayout fl;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
HalmaAndroid.GAService.GetGASInstance().Initialize_NativeGAS(this);
var g = new Game1();
//Game engine activity variable
//g.activity = this;
interstitial = AdWrapper.ConstructFullPageAd(this, interstitialID);
fl = new FrameLayout(this);
fl.AddView((View)g.Services.GetService(typeof(View)));
SetContentView(fl);
g.Run();
}
public void CreateBannerAd()
{
if (ll == null)
{
HideBannerAd();
ll = new LinearLayout(this);
ll.Orientation = Orientation.Horizontal;
ll.SetGravity(GravityFlags.Left | GravityFlags.Bottom);
bannerAd = new AdView(this);
bannerAd.AdSize = AdSize.Banner;
bannerAd.AdUnitId = bannerID;
ll.AddView(bannerAd);
fl.AddView(ll);
}
bannerAd.CustomBuild(testDeviceID);
}
public void HideBannerAd()
{
if (ll != null)
{
fl.RemoveView(ll);
ll.RemoveView(bannerAd);
bannerAd.Dispose();
bannerAd = null;
ll.Dispose();
ll = null;
}
}
public void ShowInterstitialAd()
{
RunOnUiThread(() =>
{
if (interstitial.AdListener != null)
interstitial.AdListener.Dispose();
interstitial.AdListener = null;
var intlistener = new adlistener();
intlistener.AdLoaded += () => { if (interstitial.IsLoaded) interstitial.Show(); };
interstitial.AdListener = intlistener;
interstitial.CustomBuild(testDeviceID);
});
}
public static class AdWrapper
{
public static InterstitialAd ConstructFullPageAd(Context con, string UnitID)
{
var ad = new InterstitialAd(con);
ad.AdUnitId = UnitID;
return ad;
}
public static AdView ConstructStandardBanner(Context con, AdSize adsize, string UnitID)
{
var ad = new AdView(con);
ad.AdSize = adsize;
ad.AdUnitId = UnitID;
return ad;
}
public static InterstitialAd CustomBuild(this InterstitialAd ad, string testDeviceID)
{
var requestbuilder = new AdRequest.Builder()
.AddTestDevice(AdRequest.DeviceIdEmulator)
.AddTestDevice(testDeviceID)
.Build();
ad.LoadAd(requestbuilder);
return ad;
}
public static AdView CustomBuild(this AdView ad, string testDeviceID)
{
var requestbuilder = new AdRequest.Builder()
.AddTestDevice(AdRequest.DeviceIdEmulator)
.AddTestDevice(testDeviceID)
.Build();
ad.LoadAd(requestbuilder);
return ad;
}
}
// show fullscreen banner
var act = (Activity1)Game1.Activity;
act.ShowInterstitialAd();
//show ads
var act = (Activity1)Game1.Activity;
act.CreateBannerAd();
//hide ads
var act = (Activity1)Game1.Activity;
act.HideBannerAd();
in AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> -->
<!-- Optional -->
<!-- <uses-permission android:name="android.permission.GET_ACCOUNTS" /> -->
<application android:label="AG_Checkers" android:icon="@drawable/Icon">
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>