gpt4 book ai didi

android - AdMob 在隐藏/显示 AdView 后不刷新广告

转载 作者:行者123 更新时间:2023-11-29 17:38:29 26 4
gpt4 key购买 nike

我想将 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.

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 可见,并在用户失去互联网连接时隐藏它。我的方法有什么问题?如果没有互联网连接状态监听器,是否可以执行此操作?

最佳答案

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

关于android - AdMob 在隐藏/显示 AdView 后不刷新广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29667164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com