3
A
回答
1
对于Xamarin.Android,有两个SDK和分步示例Google AdMob。您将需要Xamarin.GooglePlaySerives.Ads nuget。
我用它在Google Play上发布的Xamarin.Forms应用中显示广告。
这是给你的应用程序的Android部分的示例代码:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Android.Gms.Ads;
using Android;
namespace AdMobExample
{
[Activity (Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected AdView mAdView;
protected InterstitialAd mInterstitialAd;
protected Button mLoadInterstitialButton;
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.activity_main);
mAdView = FindViewById<AdView> (Resource.Id.adView);
var adRequest = new AdRequest.Builder().Build();
mAdView.LoadAd (adRequest);
mInterstitialAd = new InterstitialAd (this);
mInterstitialAd.AdUnitId = GetString (Resource.String.test_interstitial_ad_unit_id);
mInterstitialAd.AdListener = new AdListener (this);
mLoadInterstitialButton = FindViewById<Button> (Resource.Id.load_interstitial_button);
mLoadInterstitialButton.SetOnClickListener (new OnClickListener (this));
}
protected void RequestNewInterstitial()
{
var adRequest = new AdRequest.Builder().Build();
mInterstitialAd.LoadAd (adRequest);
}
protected void BeginSecondActivity()
{
var intent = new Intent (this, typeof(SecondActivity));
StartActivity (intent);
}
protected override void OnPause()
{
if (mAdView != null) {
mAdView.Pause();
}
base.OnPause();
}
protected override void OnResume()
{
base.OnResume();
if (mAdView != null) {
mAdView.Resume();
}
if (!mInterstitialAd.IsLoaded) {
RequestNewInterstitial();
}
}
protected override void OnDestroy()
{
if (mAdView != null) {
mAdView.Destroy();
}
base.OnDestroy();
}
class AdListener : Android.Gms.Ads.AdListener
{
MainActivity that;
public AdListener (MainActivity t)
{
that = t;
}
public override void OnAdClosed()
{
that.RequestNewInterstitial();
that.BeginSecondActivity();
}
}
class OnClickListener : Java.Lang.Object, View.IOnClickListener
{
MainActivity that;
public OnClickListener (MainActivity t)
{
that = t;
}
public void OnClick (View v)
{
if (that.mInterstitialAd.IsLoaded) {
that.mInterstitialAd.Show();
} else {
that.BeginSecondActivity();
}
}
}
}
}
还有一个STE-一步的指导,让AdMob广告Xamarin.iOS:
using Google.MobileAds;
...
const string intersitialId = "<Get your ID at google.com/ads/admob>";
Interstitial adInterstitial;
public override void ViewDidLoad()
{
base.ViewDidLoad();
CreateAndRequestInterstitial();
}
public void AfterSomeTime()
{
if (adInterstitial.IsReady)
adInterstitial.PresentFromRootViewController (navController);
}
void CreateAndRequestInterstitial()
{
adInterstitial = new Interstitial (intersitialId);
adInterstitial.ScreenDismissed += (sender, e) => {
// Interstitial is a one time use object. That means once an interstitial is shown, HasBeenUsed
// returns true and the interstitial can't be used to load another ad.
// To request another interstitial, you'll need to create a new Interstitial object.
adInterstitial.Dispose();
adInterstitial = null;
CreateAndRequestInterstitial();
};
var request = Request.GetDefaultRequest();
// Requests test ads on devices you specify. Your test device ID is printed to the console when
// an ad request is made. GADBannerView automatically returns test ads when running on a
// simulator. After you get your device ID, add it here
request.TestDevices = new [] { Request.SimulatorId.ToString() };
adInterstitial.LoadRequest (request);
}
相关问题
- 1. Heroku广告横幅?
- 2. Flash横幅广告规格
- 3. 广告横幅不显示
- 4. 安卓广告横幅
- 5. 自定义广告横幅
- 6. 让横幅广告响应
- 7. 横幅广告不显示
- 8. 放横幅广告behing Adsense
- 9. Relativelayout检查横幅广告?
- 10. TableView内的广告横幅
- 11. 如果横幅广告移动,iAd横幅不可点击
- 12. 下拉横幅广告需要
- 13. 在MediaWiki上使用广告和横幅
- 14. Admob横幅广告尚未加载SherlockFragmentActivity
- 15. 在RecyclerView底部显示横幅广告?
- 16. Flash横幅广告链接在Chrome
- 17. iAd没有显示横幅广告
- 18. Leadbolt sdk横幅广告未显示
- 19. 广告横幅空间不足
- 20. tableView Cell在admob横幅广告后面
- 21. 延迟从admob加载横幅广告
- 22. 用SpriteKit显示横幅广告
- 23. android:乱舞广告横幅全屏
- 24. 刷新leadbolt html横幅广告
- 25. 在线广告/横幅管理系统
- 26. AdMob横幅广告未显示Swift 3
- 27. ios智能横幅广告adwhril或mobclix
- 28. 网页上的横幅广告
- 29. 无广告时隐藏iAd横幅
- 30. 广告横幅块UITableView的底部
它会显示广告在所有页面中,还是仅引用MainActivity? –
@ChandreshKhambhayata你需要自己完成代码。这是一个示例代码,它非常简单。它不适合盲文复制粘贴。 –
当屏幕方向改变时,你有没有遇到过问题?目前在iOS上,只要设备从纵向翻转到横向或背面广告消失,我认为它会从屏幕上消失,或者尺寸变得混乱或者某种东西,但我无法弄清楚。试图使用智能横幅,顺便说一句。 – hvaughan3