我有一个用android sdk制作的简单游戏。当用户级别失败时,我希望在当时或多或少准确地显示插页式广告。问题来自我的代码,从插页式函数开始到间隙出现的时刻是@ 5秒延迟(在LogCat中检查。这是代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (getResources().getString(R.string.InterstitialAd_unit_id).length() > 0) {
// Create the interstitial
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(getResources().getString(R.string.InterstitialAd_unit_id));
// Create ad request.
adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
}
//initialise banner ad
this.BANNER_AD_UNIT_ID = getResources().getString(R.string.BannerAd_unit_id);
showBanner();
}
public void openAd() {
if (getResources().getString(R.string.InterstitialAd_unit_id).length() > 0) {
runOnUiThread(new Runnable() {
public void run() {
if (!interstitial.isLoaded()) {
interstitial.loadAd(adRequest);
}
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
interstitial.show();
}
});
}
});
}
}
有没有办法在调用该函数之前缓存插页式广告。那种延误真的很糟糕...... 谢谢!
答案 0 :(得分:3)
您的问题是您正在尝试加载广告并同时展示广告。加载需要一个缓慢的网络请求。这永远不会给你你所经历的经验。
建议的做法是在游戏或关卡开始时拨打interstitial.loadAd(adRequest)
。然后在你的等级电话结束时
if (interstitial.isAdLoaded() {
interstitial.show();
}