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);
}
});
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;
}}
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.
}
});
}}
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);
...} }
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>
And ask for these permissions:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
If your question is to know, shall I load the ad or not based on the conditions required for the ad is satisfied like internet and GooglePlayServices
, then you can use the method below:
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();
}
The method shouldLoadAd checks if internet and Googleplayservices is available then load ad else dont load ad, if it returns false you can hide the adView.