我正在尝试在游戏中添加横幅广告。我可以在加载时显示广告,但问题是当我使Adview对象不可见时,它不在后台加载。广告仅在Adview对象的可见性设置为Invisible时加载,但对于我的游戏,我必须在退出屏幕上显示广告。
提前致谢
代码:
public void initAds()
{
rect_layout = (FrameLayout) findViewById(R.id.rectangleView);
banner_layout = (LinearLayout) findViewById(R.id.bannerView);
banner_ad = new AdView(_activity);
banner_ad.setAdUnitId(BANNER_AD_ID);
banner_ad.setAdSize(AdSize.SMART_BANNER);
AdRequest adRequestBanner = new AdRequest.Builder().build();
banner_ad.loadAd(adRequestBanner);
banner_layout.addView(banner_ad);
rect_ad = new AdView(_activity);
rect_ad.setAdUnitId(RECTANGLE_AD_ID);
rect_ad.setAdSize(AdSize.MEDIUM_RECTANGLE);
AdRequest adRequestRectangle = new AdRequest.Builder().build();
rect_ad.loadAd(adRequestRectangle);
rect_layout.addView(rect_ad);
rect_ad.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
}
@Override
public void onAdLeftApplication() {
super.onAdLeftApplication();
}
@Override
public void onAdOpened() {
super.onAdOpened();
}
@Override
public void onAdLoaded() {
super.onAdLoaded();
//rect_layout.setVisibility(View.INVISIBLE);
}
});
//rect_layout.setAlpha(0);
}
//Rectangular ad
public static void showRectangularAd(final String _show)
{
_activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if(Boolean.parseBoolean(_show))
{
_activity.rect_layout.setVisibility(View.VISIBLE);
//_activity.rect_layout.setAlpha(1);
Log.d(TAG, "Set to visible");
}
else
{
_activity.rect_layout.setVisibility(View.INVISIBLE);
//_activity.rect_layout.setAlpha(0);
Log.d(TAG, "Set to invisible");
}
}
});
}
答案 0 :(得分:0)
首先,当处于不可见状态时,您无法加载广告视图。实际上谷歌会跟踪您的广告观看次数并将广告加载到广告视图,只有广告视图可见。
因此,您必须保持广告素材可见,以便加载。
你可以做这样的事。 强制用户等待广告加载完成。 (仅当广告加载完成后才显示退出按钮)。