Reputation: 41
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();
}
}
What's wrong with this code? I tried several things but behaviour is the same. For example, it also won't load ads if Wi-Fi router was rebooted during session until game is closed and reopened as a new process.
Upvotes: 1
Views: 465
Reputation: 12823
Try using an AlarmManager or JobManager to schedule a retry of admobView.loadAd(adRequest)
from within your onAdFailedToLoad()
method.
You could also check network connectivity before attempting to load your ad and use a CONNECTIVITY_CHANGED
broadcast receiver to attempt the load once connectivity is returned.
As-is you're only calling loadAd()
from onCreate()
. Having to leave/re-open the app to get the ad to load is to be expected.
Upvotes: 1