I would like to add AdMob ads to my application, but I have a problem with showing/hiding them depending on the internet connection status of the device. When I turn on my device with WiFi turned on the ad is shown correctly, when I turn the WiFi off, the ad is hiding also corectly. But when I go from "no internet" state to WiFi/3G on the ad is not showing. Instead I get suchmessages in Logcat:
04-15 13:16:03.688 8813-8813/com.package.app I/Ads﹕ Ad is not visible. Not refreshing ad.
04-15 13:16:03.688 8813-8813/com.package.app I/Ads﹕ Scheduling ad refresh 60000 milliseconds from now.
Activity's code:
@Override
protected void onCreate(Bundle savedInstanceState) {
/***/
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
mAdView.setVisibility(View.VISIBLE);
super.onAdLoaded();
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
mAdView.setVisibility(View.GONE);
}
});
}
@Override
protected void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}
@Override
protected void onPause() {
super.onPause();
if (mAdView != null) {
mAdView.pause();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mAdView != null) {
mAdView.destroy();
}
}
I would like to turn the ads on and make the AdView
visible when the user turns the internet connection on, and hide it when the user loses the internet connection. What's wrong with my approach? Is it possibile to do this without internet connectivity state listener?