FragmentActivity和Admob广告没有出现?

时间:2016-09-15 15:53:48

标签: c# android xamarin xamarin.android admob

我的广告没有显示。 它总是说我的广告没有加载我宣布的Toast。

这是我的代码:

这是我的按钮,用于在我的选项菜单中显示广告 此选项将加载自定义alertdialog,并且在此alertdialog中有一个按钮,如果您单击此按钮,广告应该会显示但不会显示:

           //Optionsmenü
    public override bool OnPrepareOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.profile_menu, menu);
        RelativeLayout badgeLayout = (RelativeLayout)menu.FindItem(Resource.Id.diamonds).ActionView;
        TextView mCounter = badgeLayout.FindViewById<TextView>(Resource.Id.diamondcounter);
        mCounter.Text = diamonds;
        Button startdiamonds = badgeLayout.FindViewById<Button>(Resource.Id.startdiamonds);
        startdiamonds.Click += delegate
        {
            View content = LayoutInflater.Inflate(Resource.Layout.GetDiamond, null);
            AlertDialog.Builder alert = new AlertDialog.Builder(this, Resource.Style.MyAlertDialogStyle);
            alert.SetView(content);
            ImageButton freegem = content.FindViewById<ImageButton>(Resource.Id.ButtonGemAd);
            freegem.Click += delegate
            {
                //Interstitial Ad
                mInterstitialAd = new InterstitialAd(this);
                mInterstitialAd.AdUnitId = AD_UNIT_ID1;
                mInterstitialAd.AdListener = new adlistener(this);
                var adRequest = new AdRequest.Builder().Build();
                mInterstitialAd.LoadAd(adRequest);
                if (mInterstitialAd.IsLoaded)
                {
                    mInterstitialAd.Show();
                }
                else
                {
                    Toast.MakeText(this, "Ad not ready yet! Please wait a few seconds!", ToastLength.Long).Show();
                }
            };
            alert.SetPositiveButton(" Cancle", (senderAlert, args) => {  });
            Dialog dialog = alert.Create();
            dialog.Show();
        };

        return base.OnPrepareOptionsMenu(menu);
    }

这是我的AdAdpater:

   //AdListener für AdClosed
class adlistener : AdListener
{
    private ProfileActivity profileActivity;
    public delegate void AdLoadedEvent();
    public delegate void AdClosedEvent();
    public delegate void AdOpenedEvent();
    public event AdLoadedEvent AdLoaded;
    public event AdClosedEvent AdClosed;
    public event AdOpenedEvent AdOpened;

    public adlistener(ProfileActivity profileActivity)
    {
        this.profileActivity = profileActivity;
    }

    public override void OnAdOpened()
    {
        if (AdOpened != null) this.AdOpened();
        base.OnAdOpened();
    }

    public override void OnAdClosed()
    {
        if (AdClosed != null) this.AdClosed();
        base.OnAdClosed();

    }

    public override void OnAdLoaded()
    {
        if (AdLoaded != null) this.AdLoaded();
        base.OnAdLoaded();

    }
}

编辑:

代码完美无缺我在Android Manifest中只错过了这个:

 <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
    <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

1 个答案:

答案 0 :(得分:0)

更改freegem.Click + = delegate中的代码  到LoadAd而不是ShowAd()

interstitialAd.LoadAd(adRequest);

您可以随时调用LoadAd,但是,在显示广告素材之前,您必须等待提升InterstitialAd的ReceivedAd事件。如果引发了FailedToReceiveAd事件,请正常处理错误情况。

收到广告后,您可以使用ShowAd方法展示广告。然后,插页式广告会占据屏幕,直到用户将其解散为止,此时控件会返回到您的应用。

private void OnAdReceived(object sender, AdEventArgs e)
       {
           System.Diagnostics.Debug.WriteLine("Ad received successfully");
           interstitialAd.ShowAd();
       }