广告仅在应用启动前启用WI-FI时显示(如果在应用会话期间启用了Wi-Fi,则不会显示)。如何在可能的情况下加载Admob广告?我相信,这与AdListener有关。 我目前的代码:
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
adView.setVisibility(View.VISIBLE);
super.onAdLoaded();
}
public void onAdFailedToLoad(int errorCode) {
adView.setVisibility(View.GONE);
}
});
答案 0 :(得分:0)
如果您的问题需要了解,我是否应根据互联网和GooglePlayServices
等广告所需的条件加载广告,然后您可以使用以下方法:
public static boolean shouldLoadAd(Context context) {
return (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS && isNetworkAvailable(context));
}
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
方法shouldLoadAd会检查互联网和Googleplayservices是否可用,然后加载广告,否则不加载广告,如果返回false,则可以隐藏广告。
答案 1 :(得分:0)
您需要一个BroadCastReceiver来知道网络状态何时更改,以便您可以加载您的添加内容。
public class NetworkChangeReceiver extends BroadcastReceiver {
public static final int TYPE_WIFI = 1;
public static final int TYPE_MOBILE = 2;
public static final int TYPE_NOT_CONNECTED = 0;
public static final int NETWORK_STATUS_NOT_CONNECTED = 0;
public static final int NETWORK_STATUS_WIFI = 1;
public static final int NETWORK_STATUS_MOBILE = 2;
@Override
public void onReceive(Context context, Intent intent) {
int status = getConnectivityStatusString(context);
if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
if (status == NETWORK_STATUS_NOT_CONNECTED) {
Log.i("Receiver","not connected");
} else {
Log.i("Receiver","connected");
//If the connectivity changes while using the app and it is connected to a network you can load your adds.
AddListener.initialiseAds(mAdView, adRequest);
}
}
}
public int getConnectivityStatusString(Context context) {
int conn = getConnectivityStatus(context);
int status = 0;
if (conn == TYPE_WIFI ) {
status = NETWORK_STATUS_WIFI;
} else if (conn == TYPE_MOBILE ) {
status = NETWORK_STATUS_MOBILE;
} else if (conn == TYPE_NOT_CONNECTED) {
status = NETWORK_STATUS_NOT_CONNECTED;
}
return status;
}
public int getConnectivityStatus(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
return TYPE_WIFI;
if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
return TYPE_MOBILE;
}
return TYPE_NOT_CONNECTED;
}}
然后我们有AddListener类
public final class AddListener {
private AddListener(){
}
public static void initialiseAds(final AdView mAdView, AdRequest adRequest) {
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
if (mAdView.getVisibility() == View.GONE) {
mAdView.setVisibility(View.VISIBLE);
}
Log.i("Receiver","Add loaded");
// Code to be executed when an ad finishes loading.
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
mAdView.setVisibility(View.GONE);
Log.i("Receiver","Add error "+errorCode);
// Code to be executed when an ad request fails.
}
@Override
public void onAdOpened() {
Log.i("Receiver","Add opened");
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
@Override
public void onAdLeftApplication() {
Log.i("Receiver","Add left app");
// Code to be executed when the user has left the app.
}
@Override
public void onAdClosed() {
Log.i("Receiver","Add closed");
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
}
});
}}
在MainUI类中,我们执行以下操作:
public class MainUI extends AppCompatActivity {
...
public static AdView mAdView;
public static AdRequest adRequest;
...
@Override
protected void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
if(mAdView!=null){ // Check if Adview is not null in case of fist time load.
mAdView.resume();}
}
@Override
protected void onPause() {
if (mAdView != null) {
mAdView.pause();
Log.i("pause","pause view");
}
super.onPause();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_ui);
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
mAdView = (AdView) findViewById(R.id.adView);
mAdView.setVisibility(View.GONE);
adRequest = new AdRequest.
Builder()
.addTestDevice("DDF43F508E61CBB2ACA96D879C66AB42")
.build();
AddListener.initialiseAds(mAdView,adRequest);
...} }
还请记住在清单中注册您的BroadcastReceiver
<receiver
android:name=".utils.NetworkTools.NetworkChangeReceiver"
android:label="NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
并要求这些权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />