隐藏/显示AdView后,AdMob不会刷新广告

时间:2015-04-16 06:41:56

标签: android admob

我想将AdMob广告添加到我的应用程序中,但根据设备的互联网连接状态显示/隐藏它们时出现问题。当我打开WiFi打开设备时,广告显示正确,当我关闭WiFi时,广告也隐藏了。但是,当我从“无网络”状态转到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.

活动代码:

@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,并在用户丢失互联网连接时将其隐藏。我的做法有什么问题?没有互联网连接状态监听器可以做到这一点吗?

3 个答案:

答案 0 :(得分:2)

如果有人仍然有问题,那么此代码将检查每个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);
    }
}

答案 1 :(得分:1)

如果您想检测用户何时更改其连接,您应该注册BroadcastReceiver,然后检查this
隐藏/显示AdView,我使用一个RelativeLayout作为持有人,我隐藏它而不是AdView

答案 2 :(得分:0)

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

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

  }