gpt4 book ai didi

android - Admob 插页式广告在断点处显示

转载 作者:行者123 更新时间:2023-11-30 01:34:11 26 4
gpt4 key购买 nike

我的游戏中有一个 GameOverActivity,我想在其 onCreate() 中显示一个 InterstitialAd。为此,我需要将它加载到其他地方,因为它不会显示。我该怎么做?

protected void onCreate(Bundle savedInstanceState){
//admob
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
//requestNewInterstitial
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
onResume();
}
});
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
onPause();
}
}

最佳答案

您的代码无法运行,因为插页式广告需要一些时间才能下载。所以,Google 建议先下载后显示。

所以,我认为你应该在调用 GameOverActivity 之前显示它(下载它并在之前的 Activity 上显示它..

例如:

在上一个 Activity 的 onCreate() 函数期间,您请求广告(它将被下载,这需要一些时间)。

然后,在启动 GameOverActivity 之前,首先显示 InterstitialAd。广告关闭后,您将启动 GameOverActivity。

public class MainActivity {
protected void onCreate(Bundle savedInstanceState) {
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("AD_UNIT_ID");
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// Start GameOverActivity when the ad is closed
Intent intent = new Intent(this, GameOverActivity.class);
startActivity(intent);
}
});

AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}

// You probably have some function from where you start GameOverActivity
private void methodCalledWhenUserLoses() {
if (mInterstitialAd.isLoaded()) {
// If loaded, show it. GameOverActivity will be started when ad is closed
mInterstitialAd.show();
} else {
// If ad was not loaded yet, shows show GameOverActivity
Intent intent = new Intent(this, GameOverActivity.class);
startActivity(intent);
}
}

}

上面的代码是一个例子。

可在以下位置找到插页式广告示例:

https://developers.google.com/mobile-ads-sdk/docs/dfp/android/interstitial

关于android - Admob 插页式广告在断点处显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35319460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com