Reputation: 617
I use GPS to provide ads in my application, but there are blank box until ad is loaded and also when user doesn't have access to te Internet and it's look awful. This is i think changed behaviour in GPS SDK in migration from dedicated AdMob SDK (which is deprecated).
I tried out set listeners for onAdLoaded, which set visibility to visible after load from gone i set after create, but there is a serious problem. This is doesn't show a blank box on application start and show ad after her load. However when there are not access to the Internet, onAdLoaded don't fired anytime because of hidden Ad! Also return these logs:
Ad is not visible. Not refreshing ad.
Scheduling ad refresh 60000 milliseconds from now.
In this situation, user never sees the ad... How can I prevent GPS Ads doing this?
There is my code placed in main activity:
adView = new AdView(this);
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
adView.setVisibility(View.VISIBLE);
super.onAdLoaded();
}
});
adView.setVisibility(View.GONE);
adView.setAdUnitId("xxx");
adView.setAdSize(AdSize.SMART_BANNER);
layout = super.root;
layout.addView(adView);
adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
Upvotes: 0
Views: 1739
Reputation: 617
The most easiest thing we can do is don't do anything... But result is ugly, blank box isn't sexy.
Anything will change, if we place "visible" adView into a hidden container, situation is same and AdMob recognize that adView is not visible in fact, so 'Ad is not visible. Not refreshing ad.' Thus the container is superflous.
The solution is comparatively long and consist in recreating the adView after onAdFailedToLoad fire until the first ad will be loaded. My solution is derived from Dan Dragut's nice solution posted here and I removed the containter, made code slighty shorter and did it cordova compatible. I do use nonfixed orientation together with use smart_banner size, thus is needed to handle onOrientationChange event (and recreate adView). So here it is:
public class example extends DroidGap
{
private Ad ad; // new ad class because of code size
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set by <content src="index.html" /> in config.xml
super.loadUrl(Config.getStartUrl());
appView.getSettings().setRenderPriority(RenderPriority.HIGH);
ad = new Ad(this, super.root);
}
@Override
public void onConfigurationChanged(Configuration myConfig) {
super.onConfigurationChanged(myConfig);
ad.restart();
}
@Override
public void onPause() {
ad.pause();
super.onPause();
}
@Override
public void onResume() {
super.onResume();
ad.resume();
}
private class Ad {
private DroidGap mGap;
private AdView adView;
private LinearLayout layout;
private AdRequest adRequest;
private Handler handler = new Handler();
public Ad(DroidGap _mGap, LinearLayout _layout) {
mGap = _mGap;
layout = _layout;
adRequest = new AdRequest.Builder().build();
recreate();
}
public void restart() {
handler.removeCallbacksAndMessages(null);
layout.removeView(adView);
adView.destroy();
this.recreate();
}
public void pause() {
adView.pause();
}
public void resume() {
adView.resume();
}
private void recreate() {
adView = new AdView(mGap);
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
adView.setVisibility(View.VISIBLE);
}
@Override
public void onAdFailedToLoad(int errorCode) {
//Log.e(TAG, String.format("onAdFailedToLoad(%s)", errorCode));
// Refresh it ourselves...
handler.removeCallbacksAndMessages(null);
handler.postDelayed(new Runnable() {
@Override
public void run() {
restart();
}
}, 30000); // delay of recreating ad
}
});
adView.setVisibility(View.GONE);
adView.setAdUnitId("xxx");
adView.setAdSize(AdSize.SMART_BANNER);
// Lookup your LinearLayout assuming it's been given
// the attribute android:id="@+id/mainLayout".
// Add the adView to it.
layout.addView(adView);
// Initiate a generic request.
// Load the adView with the ad request.
adView.loadAd(adRequest);
}
}
}
Upvotes: 0
Reputation: 20196
You can set up a BroadcastReceiver listening for android.net.conn.CONNECTIVITY_CHANGE
When you have a network connection again you can make your Ad visible.
Or you can do what the rest of us do and not worry about an occassional blank AdView.
Upvotes: 0