如果首次加载期间没有连接,AdMob广告横幅将不会显示

时间:2015-06-23 15:33:07

标签: android admob

public class AndroidLauncher extends AndroidApplication {

protected View gameView;
protected AdView adView;
protected RelativeLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
    cfg.useImmersiveMode = true;


    layout = new RelativeLayout(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    layout.setLayoutParams(params);

    AdView admobView = createAdView();
    layout.addView(admobView);
    View gameView = createGameView(cfg);
    layout.addView(gameView);

    setContentView(layout);

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


private AdView createAdView() {
    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId("**actual_id****");
    adView.setId(R.id.admob);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    adView.setLayoutParams(params);
    adView.setBackgroundColor(Color.BLACK);
    adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            adView.setVisibility(View.VISIBLE);
            super.onAdLoaded();
        }

        public void onAdFailedToLoad(int errorCode) {
            adView.setVisibility(View.GONE);
            super.onAdFailedToLoad(errorCode);
        }
    });
    //  adView.setVisibility(View.GONE);

    return adView;
}

private View createGameView(AndroidApplicationConfiguration cfg) {
    gameView = initializeForView(new GameName(), cfg);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.BELOW, adView.getId());
    gameView.setLayoutParams(params);
    return gameView;
}


@Override
public void onResume() {
    super.onResume();
    if (adView != null) adView.resume();
}

@Override
public void onPause() {
    if (adView != null) adView.pause();
    super.onPause();
}

@Override
public void onDestroy() {
    if (adView != null) adView.destroy();
    super.onDestroy();
}
}

这段代码有什么问题?我尝试了几件事,但行为是一样的。例如,如果在会话期间重新启动Wi-Fi路由器,直到游戏关闭并重新打开为新流程,它也不会加载广告。

1 个答案:

答案 0 :(得分:0)

尝试使用AlarmManager或JobManager安排从admobView.loadAd(adRequest)方法中重试onAdFailedToLoad()

您还可以在尝试加载广告之前检查网络连接,并在返回连接后使用CONNECTIVITY_CHANGED广播接收器尝试加载。

按原样,您只能从loadAd()拨打onCreate()。必须离开/重新打开应用才能让广告加载。