简体   繁体   中英

Ad Close Event For InterstitialAd Xamarin forms

i am implemeting google interstitial ads in my app ads are showing nice and good i just want to get the ad close event for this ad i searched through internet but i did not get any ad close event for this ad here is my code

my interface

public  interface IAdInterstitial
{
    void ShowAd();
    void LoadInterstitialAd();
}

my android custom renderer

public class AdInterstitial_Droid : IAdInterstitial
{
    InterstitialAd interstitialAd;

    public AdInterstitial_Droid()
    {
        interstitialAd = new InterstitialAd(Android.App.Application.Context);

        // TODO: change this id to your admob id  
        interstitialAd.AdUnitId = "ca-app-pub-3940256099942544/1033173712";
        LoadAd();
    }

   public void LoadAd()
    {
        var requestbuilder = new AdRequest.Builder();
        interstitialAd.LoadAd(requestbuilder.Build());
    }

    public void ShowAd()
    {
        if (interstitialAd.IsLoaded)
            interstitialAd.Show();

        LoadAd();
    }

    public void LoadInterstitialAd()
    {
        var requestbuilder = new AdRequest.Builder();
        interstitialAd.LoadAd(requestbuilder.Build());
    }
}

how i can get the ad close event for this ad so that i can do some stuff on ad close

You can create your own AdListener subclass that listens for the OnAdClosed event and than invokes your own action:

Example usage:

public AdInterstitial_Droid()
{
    interstitialAd = new InterstitialAd(Android.App.Application.Context);
    interstitialAd.AdListener = new MyAdListener(() =>
    {
        // Ad closing, do whatever you need to do
    });

    // TODO: change this id to your admob id  
    interstitialAd.AdUnitId = "xxxxx";
    LoadAd();
}

AdListener subclass

public class MyAdListener : AdListener
{
    Action OnCloseAction;

    public MyAdListener(Action OnCloseAction)
    {
        this.OnCloseAction = OnCloseAction;
    }

    public override void OnAdClosed()
    {
        OnCloseAction?.Invoke();
        base.OnAdClosed();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM