InterstitialAd

public abstract class InterstitialAd

已知的直接子类
AdManagerInterstitialAd

一种面向 Google Ad Manager 发布商的广告,可在自然过渡点(比如页面切换、应用启动或游戏关卡加载)展示整页广告体验。


一种在自然过渡点(比如页面切换、应用启动或游戏关卡加载)展示的整页广告体验。插页式广告借助关闭按钮来帮助用户清除广告。

示例代码:

public class MyActivity extends Activity {
    private InterstitialAd interstitialAd;
    private Button nextLevelButton;
    private TextView textView;

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

        // Create a full screen content callback.
        FullScreenContentCallback fullScreenContentCallback = new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
               interstitialAd = null;
               // Proceed to the next level.
               goToNextLevel();
            }
        };

        // Load an interstitial ad. When a natural transition in the app occurs (such as a level
        // ending in a game), show the interstitial. In this simple example, the press of a
        // button is used instead.
        //
        // If the button is clicked before the interstitial is loaded, the user should proceed to
        // the next part of the app (in this case, the next level).
        //
        // If the interstitial is finished loading, the user will view the interstitial before
        // proceeding.
        InterstitialAd.load(
            this,
            "myAdUnitId",
            new AdRequest.Builder().build(),
            new InterstitialAdLoadCallback() {
                @Override
                public void onAdLoaded(@NonNull InterstitialAd ad) {
                    interstitialAd = ad;
                    interstitialAd.setFullScreenContentCallback(fullScreenContentCallback);
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError adError) {
                    // Code to be executed when an ad request fails.
                }
            });

        // Create the button to go to the next level.
        nextLevelButton = new Button(this);
        nextLevelButton.setText("Next Level");
        nextLevelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Show the interstitial if it is ready. Otherwise, proceed to the next level
                // without ever showing it.
                if (interstitialAd != null) {
                    interstitialAd.show(MyActivity.this);
                } else {
                    // Proceed to the next level.
                    goToNextLevel();
                }
            }
        });

        // Add the next level button to the layout.
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(nextLevelButton);

        // Create a TextView to display the current level.
        textView = new TextView(this);
        textView.setText("Level 1");
        layout.addView(textView);

        setContentView(layout);
    }

    public void goToNextLevel() {
        // Show the next level, and disable the next level button since there are no more levels.
        nextLevelButton.setEnabled(false);
        textView.setText("Level 2");
    }
}

摘要

公共构造函数

公共方法

abstract @NonNull String

返回广告单元 ID。

abstract @Nullable FullScreenContentCallback

获取此 InterstitialAdFullScreenContentCallback

abstract @Nullable OnPaidEventListener

获取此 InterstitialAdOnPaidEventListener

abstract @NonNull ResponseInfo

返回已加载广告的 ResponseInfo 对象。

static boolean
isAdAvailable(@NonNull Context context, @NonNull String adUnitId)

如果有可从 startPreload 加载的插页式广告,则返回 true

static void
load(
    @NonNull Context context,
    @NonNull String adUnitId,
    @NonNull AdRequest adRequest,
    @NonNull InterstitialAdLoadCallback loadCallback
)

加载 InterstitialAd

static @Nullable InterstitialAd
pollAd(@NonNull Context context, @NonNull String adUnitId)

检索从 startPreload 加载的下一个插页式广告,如果没有可用的广告,则检索 null

abstract void

注册一个回调,以便在广告展示和关闭全屏内容时调用。

abstract void
setImmersiveMode(boolean immersiveModeEnabled)

设置一个标志,用于控制此插页式广告对象是否会以沉浸式模式显示。

abstract void

注册一个回调,以便在系统估算此广告赚取了收入时调用该回调。

abstract void
show(@NonNull Activity activity)

展示插页式广告。

公共构造函数

InterstitialAd

public InterstitialAd()

公共方法

getAdUnitId

public abstract @NonNull String getAdUnitId()

返回广告单元 ID。

getFullScreenContentCallback

public abstract @Nullable FullScreenContentCallback getFullScreenContentCallback()

获取此 InterstitialAdFullScreenContentCallback

getOnPaidEventListener

public abstract @Nullable OnPaidEventListener getOnPaidEventListener()

获取此 InterstitialAdOnPaidEventListener

getResponseInfo

public abstract @NonNull ResponseInfo getResponseInfo()

返回已加载广告的 ResponseInfo 对象。在广告成功加载之前,返回 null

isAdAvailable

public static boolean isAdAvailable(@NonNull Context context, @NonNull String adUnitId)

如果有可从 startPreload 加载的插页式广告,则返回 true

负荷

public static void load(
    @NonNull Context context,
    @NonNull String adUnitId,
    @NonNull AdRequest adRequest,
    @NonNull InterstitialAdLoadCallback loadCallback
)

加载 InterstitialAd

参数
@NonNull Context context

activity 或应用上下文。

@NonNull String adUnitId

广告单元 ID。

@NonNull AdRequest adRequest

包含定位信息的广告请求。

@NonNull InterstitialAdLoadCallback loadCallback

在插页式广告加载完成时要调用的回调。

pollAd

public static @Nullable InterstitialAd pollAd(@NonNull Context context, @NonNull String adUnitId)

检索从 startPreload 加载的下一个插页式广告,如果没有可用的广告,则检索 null

setFullScreenContentCallback

public abstract void setFullScreenContentCallback(
    @Nullable FullScreenContentCallback fullScreenContentCallback
)

注册一个回调,以便在广告展示和关闭全屏内容时调用。

setImmersiveMode

public abstract void setImmersiveMode(boolean immersiveModeEnabled)

设置一个标志,用于控制此插页式广告对象是否会以沉浸式模式显示。请在 show 之前调用此方法。在 show 期间,如果此标志处于开启状态且支持沉浸模式,系统会为插页式广告开启 SYSTEM_UI_FLAG_IMMERSIVE_STICKY 和 SYSTEM_UI_FLAG_HIDE_NAVIGATION。

setOnPaidEventListener

public abstract void setOnPaidEventListener(@Nullable OnPaidEventListener listener)

注册一个回调,以便在系统估算此广告赚取了收入时调用该回调。

显示

public abstract void show(@NonNull Activity activity)

展示插页式广告。

参数
@NonNull Activity activity

用于展示广告的 Activity 上下文。