0

我想在应用程序类中加载广告插页式广告,然后在用户从活动 A 导航到 B 时显示该广告。如何实现这一点?

4

1 回答 1

0

您可以创建 InterstitialAdsManager 类:

public class MyInterstitialAdsManager extends InterstitialAdLoadCallback {
    private InterstitialAd mInterstitialAd;

    public MyInterstitialAdsManager(Context context) {
        loadNewInterstitialAd(context);
    }

    @Override
    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
        mInterstitialAd = interstitialAd;
        mInterstitialAd.setImmersiveMode(true);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {});
    }

    @Override
    public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        mInterstitialAd = null;
    }

    private void loadNewInterstitialAd(Context context) {
        AdRequest adRequest = new AdRequest.Builder().build();
        InterstitialAd.load(context, "your-ad-id", adRequest, this );
    }

    public void showAd(Activity activity) {
        if (mInterstitialAd != null) {
            mInterstitialAd.show(activity);
        }

        loadNewInterstitialAd(activity);
    }
}

并在您的应用程序类中使用它:

public class MyApplication extends Application {
    public InterstitialAdsManager adsManager;

    @Override
    public void onCreate() {
        super.onCreate();

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
                adsManager = new InterstitialAdsManager(this);
            }
        });
    }
}
于 2021-05-26T12:52:39.847 回答