AdMob 在 hiding/showing AdView 后不刷新广告

AdMob does not refresh the ad after hiding/showing AdView

我想将 AdMob 广告添加到我的应用程序中,但我遇到了 showing/hiding 问题,具体取决于设备的互联网连接状态。当我在打开 WiFi 的情况下打开我的设备时,广告会正确显示,当我关闭 WiFi 时,广告也会正确隐藏。但是当我从 "no internet" 状态变为 WiFi/3G 时,广告没有显示。相反,我在 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的代码:

@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();
        }
    }

我想打开广告并使 AdView 在用户打开互联网连接时可见,并在用户失去互联网连接时隐藏它。我的方法有什么问题?如果没有互联网连接状态侦听器,是否可以执行此操作?

如果你想检测用户何时改变他的连接,你应该注册一个BroadcastReceiver,勾选this
对于 hiding/showing AdView,我使用一个 RelativeLayout 作为持有人,我将其隐藏而不是 AdView

我有同样的问题,但我做了一个简单的 hack..希望这会有所帮助,但我认为这不是好方法,因为 adview 不断请求广告。

 public void onAdFailedToLoad(int errorCode) {
    super.onAdFailedToLoad(errorCode);
    adView.loadAd(adRequest);

  }

如果还有人对此有疑问,此代码将检查每个 REFRESH_RATE_IN_SECONDS 是否可以在互联网关闭时获得新广告。代码取自 here.

private int REFRESH_RATE_IN_SECONDS = 5;
AdView adView;
private final Handler refreshHandler = new Handler();
private final Runnable refreshRunnable = new RefreshRunnable();
AdRequest adRequest;

@Override
public void onCreate() {
    super.onCreate();

    adView = (AdView) findViewById(R.id.adView);
    adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)     
            .build();

    adView.setAdListener(new AdListener() {

        @Override
        public void onAdFailedToLoad(int errorCode){
            refreshHandler.removeCallbacks(refreshRunnable);
            refreshHandler.postDelayed(refreshRunnable, REFRESH_RATE_IN_SECONDS * 1000);
        }
    });

    adView.loadAd(adRequest);

}

private class RefreshRunnable implements Runnable {
    @Override
    public void run() {
        adView.loadAd(adRequest);
    }
}