从 void 方法返回延迟值

时间:2021-04-19 15:21:40

标签: java android admob

我使用了旧的 AdMob 代码,并且因为我在代码中有多个地方调用了广告,所以我使用 AdMob 代码创建了一个单独的方法。现在谷歌又在改变了,我正在为同样的东西实现新的代码。问题是我需要以某种方式从方法返回 mInterstitial 值,其中 1) 是无效的 2) 在加载之前有延迟。如果我使用简单的返回,mInterstitial 将始终为空,我不能使用 while (mInterstitial == null) {},因为它不是好的做法,而且用户当时可能没有互联网连接,我不能使用出于几乎相同的原因,某种延迟 X 秒。 那么最好的方法是什么?这必须放在它自己的类中的单独静态方法中,并将像这样调用 mInterstitialAd = loadInterstitialAd();

这是谷歌代码:

    ...    
    InterstitialAd.load(this, AD_INTERSTITIAL_ID, adRequest, new InterstitialAdLoadCallback() {
                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        // The mInterstitialAd reference will be null until
                        // an ad is loaded.                                      <-- this 
                        mInterstitialAd = interstitialAd;
//return mInterstitialAd; <--cant return from void, and cant change void because its google's code
        
                        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
                            @Override
                            public void onAdDismissedFullScreenContent() {
                                // Called when fullscreen content is dismissed.
                            }
        
                            @Override
                            public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                                // Called when fullscreen content failed to show.
                            }
        
                            @Override
                            public void onAdShowedFullScreenContent() {
                                // Called when fullscreen content is shown.
                                // Make sure to set your reference to null so you don't
                                // show it a second time.
                                mInterstitialAd = null;
                            }
                        });
                    }
        
                    @Override
                    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                        // Handle the error
                        mInterstitialAd = null;
                    }

//return mInterstitialAd; <-- this always return null because it take some time to load an ad
                });

0 个答案:

没有答案
相关问题