什么应该替换已弃用的 Facebook adView setAdListener 以获取回调?

Posted

技术标签:

【中文标题】什么应该替换已弃用的 Facebook adView setAdListener 以获取回调?【英文标题】:What should replace deprecated Facebook adView setAdListener to get callbacks? 【发布时间】:2020-01-02 09:22:04 【问题描述】:

从 v5.6.0 开始,不推荐使用 adView.setAdListener。我们现在如何接收 onAdLoaded 和 onError 回调?

【问题讨论】:

【参考方案1】:

解决办法:从5.6开始,你应该使用loadAd(AdView.AdViewLoadConfig loadAdConfig)

public void loadAd(AdView.AdViewLoadConfig loadAdConfig)

加载广告。

此方法总是立即返回。广告已加载 异步。加载时将调用控件的广告侦听器 完成或失败。

Java

AdView adView = new AdView(this, "placementId", new AdSize(width, height));

AdListener adListener = new AdListener() 
    @Override
    public void onError(Ad ad, AdError adError) 

    

    @Override
    public void onAdLoaded(Ad ad) 

    

    @Override
    public void onAdClicked(Ad ad) 

    

    @Override
    public void onLoggingImpression(Ad ad) 

    
;

AdView.AdViewLoadConfig loadAdConfig = adView.buildLoadAdConfig()
        .withAdListener(adListener)
        .build();

adView.loadAd(loadAdConfig);

科特林

val adView = AdView(this, "placementId", AdSize(width, height))

val adListener = object : AdListener 
    override fun onError(ad: Ad, adError: AdError) 

    

    override fun onAdLoaded(ad: Ad) 

    

    override fun onAdClicked(ad: Ad) 

    

    override fun onLoggingImpression(ad: Ad) 

    


val loadAdConfig = adView.buildLoadAdConfig()
    .withAdListener(adListener)
    .build()

adView.loadAd(loadAdConfig)

查找更多信息here。

【讨论】:

【参考方案2】:

横幅广告代码:解决 setAdListener 方法已弃用的问题

使用以下代码替换横幅广告的 setAdlistener() 方法:

 AdView adView = new AdView(context, context.getString(R.string.str_facebook_banner), AdSize.BANNER_HEIGHT_90);

    AdListener adListener = new AdListener() 
        @Override
        public void onError(Ad ad, AdError adError) 
            Log.e(TAG, "Fb failed :: " + adError.toString());
        

        @Override
        public void onAdLoaded(Ad ad) 
            Log.e(TAG, "onAdLoaded: ");

        

        @Override
        public void onAdClicked(Ad ad) 

        

        @Override
        public void onLoggingImpression(Ad ad) 

        
    ;


    AdView.AdViewLoadConfig loadAdConfig = adView.buildLoadAdConfig()
            .withAdListener(adListener)
            .build();
    adView.loadAd(loadAdConfig);

插页式广告代码:解决 setAdListener 方法已弃用的问题

您可以使用以下代码替换插页式广告的 setAdListener() 方法:

    InterstitialAd  interstitialAd = new InterstitialAd(context, context.getString(R.string.str_facebook_interstitial));

    InterstitialAdListener interstitialAdListener = new InterstitialAdListener() 
        @Override
        public void onInterstitialDisplayed(Ad ad) 

        

        @Override
        public void onInterstitialDismissed(Ad ad) 

        

        @Override
        public void onError(Ad ad, AdError adError) 
            Log.e(TAG, "Fb failed :: " + adError.toString());
        

        @Override
        public void onAdLoaded(Ad ad) 
            Log.e(TAG, "onAdLoaded: ");
            interstitialAd.show();
        

        @Override
        public void onAdClicked(Ad ad) 

        

        @Override
        public void onLoggingImpression(Ad ad) 

        
    ;



    interstitialAd.loadAd(interstitialAd.buildLoadAdConfig()
            .withAdListener(interstitialAdListener)
            .withCacheFlags(ALL)
            .build());

官方参考链接:https://developers.facebook.com/docs/audience-network/changelog-android#5_6_0

【讨论】:

【参考方案3】:

您的类应该实现 AdListener。 有关详细说明,请参阅此链接。 https://developers.facebook.com/docs/audience-network/reference/android/com/facebook/ads/adlistener.html/

【讨论】:

【参考方案4】:
val interstitialAdFb = com.facebook.ads.InterstitialAd(this, resources.getString(R.string.fb_interstitial_id))
    val adListener = object : com.facebook.ads.InterstitialAdListener 
        override fun onLoggingImpression(p0: Ad?) 
            TODO("Not yet implemented")
        
        override fun onInterstitialDisplayed(p0: Ad?) 
            TODO("Not yet implemented")
        
        override fun onAdClicked(p0: Ad?) 
            TODO("Not yet implemented")
        
        override fun onInterstitialDismissed(p0: Ad?) 

        

        override fun onError(p0: Ad?, p1: AdError?) 

        

        override fun onAdLoaded(p0: Ad?) 

        
    

    val loadAdConfig = interstitialAdFb.buildLoadAdConfig()
            .withAdListener(adListener)
            .build()

    interstitialAdFb.loadAd(loadAdConfig)

    //and when you want to show ad
    if (interstitialAdFb.isAdLoaded)
        interstitialAdFb.show()

【讨论】:

【参考方案5】:

遵循从此处更新的最新指南: https://developers.facebook.com/docs/audience-network/support/troubleshoot/integration

例如:

NativeAdListener nativeAdListener = new NativeAdListener() 
        @Override
        public void onMediaDownloaded(Ad ad) 
            // Native ad finished downloading all assets
            Log.e(TAG, "Native ad finished downloading all assets.");
        

        @Override
        public void onError(Ad ad, AdError adError) 
        // Native ad failed to load
            Log.e(TAG, "Native ad failed to load: " + adError.getErrorMessage());
        

        @Override
        public void onAdLoaded(Ad ad) 
            // Native ad is loaded and ready to be displayed
            Log.d(TAG, "Native ad is loaded and ready to be displayed!");
        

        @Override
        public void onAdClicked(Ad ad) 
            // Native ad clicked
            Log.d(TAG, "Native ad clicked!");
        

        @Override
        public void onLoggingImpression(Ad ad) 
            // Native ad impression
            Log.d(TAG, "Native ad impression logged!");
        
    ;

    // Request an ad
    nativeAd.loadAd(
            nativeAd.buildLoadAdConfig()
                    .withAdListener(nativeAdListener)
                    .build());

【讨论】:

以上是关于什么应该替换已弃用的 Facebook adView setAdListener 以获取回调?的主要内容,如果未能解决你的问题,请参考以下文章

什么是已弃用的 getSupportLoaderManager() 的适当替换?

替换已弃用的CdkDropListContainer

如何使用已弃用的 api 从 facebook 获得邀请的朋友?

推荐替换已弃用的 call_user_method?

Python 3 替换已弃用的 compiler.ast flatten 函数

替换已弃用的函数 mysql_connect [重复]