如何尽可能地加载Admob广告?
问题描述:
广告仅在应用启动之前启用WI-FI时显示(如果在应用会话期间启用wi-fi,则不会显示)。如何尽可能地加载Admob广告?我相信,这与AdListener有关。
我当前的代码:
Ad shows only if WI-FI was enabled before app start (it won't appear if wi-fi was enabled during app session). How to load Admob ad whenever it's possible? I believe, it's got something to do with AdListener. My current code:
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
adView.setVisibility(View.VISIBLE);
super.onAdLoaded();
}
public void onAdFailedToLoad(int errorCode) {
adView.setVisibility(View.GONE);
}
});
答
您需要BroadCastReceiver才能知道网络状态何时
You need a BroadCastReceiver to know when the network status has changed so you can load your adds.
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类
Then we have the AddListener class
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类中,我们这样做:
And from the MainUI class we do this:
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清单
Also remember to register your BroadcastReceiver in your Manifest
<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" />