我的游戏中有GameOverActivity
,我想在其InterstitialAd
中显示onCreate()
。为此,我需要将其加载到其他地方,因为它不会显示。我该怎么做?
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();
}
}
答案 0 :(得分:1)
您的代码无法使用,因为插页式广告需要一段时间才能下载。因此,Google建议先下载并稍后再显示。
所以,我认为你应该在调用GameOverActivity之前显示它(下载并在之前的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