Saturday, August 22, 2015

How to know if AdMob ad has been loaded in your Android Application

In my last blog we have discussed about adding an ad from Admob network. This is in continuation with that.


In the applications we may need to take some action if the ad is loaded and some other action if the ad is not loaded. In such cases it’s very much important to know if the Admob ad is loaded to our application or not. We can achieve using the setAdListener interface of AdView. Below is the code which can be used to achieve this:

ad.setAdListener(new AdListener() {                 
              @Override
              public void onAdLoaded() {
                    Log.d(TAG, "Ad Received");
                    //Code implementation if the ad is loaded
                       ………
                      ………
                  }
               @Override
               public void onAdFailedToLoad(int errorcode) {
                      Log.d(TAG, "Ad Not Loaded Received");
                 //Code implementation if the ad is NOT loaded
                   ………
                 ………
               }
             });

onAdLoaded is the method which is used to check if the ad is loaded. And
onAdFailedToLoad is the method used to check if the ad is not loaded. Based on these implementation you can implement your respective action code.

Also you need to fetch the ads only if you have the network connectivity otherwise it just keeps trying to fetch the ad which may slow your application. We can check the network status before we fetch and load the ads. To check the network status you can use ConnectivityManager class. 

private static final String DEBUG_TAG = "NetworkStatusExample";

...      

ConnectivityManager connMgr = (ConnectivityManager) 

        getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

boolean isWifiConn = networkInfo.isConnected();

networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

boolean isMobileConn = networkInfo.isConnected();

Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);

Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);

For this I have added a class- NetworkUtil to check the network status. You can download it from here. (NetworkUtil) and use it in your activity to check the status. You need to create an object for this class and use the method: getConnectivityStatus. Here is the code to do so:

NetworkUtil net =new NetworkUtil();
       int status = net.getConnectivityStatus(getApplicationContext());
        if(status!=0){
            //Code if the network connection is there      
        }
        else{
            //Code if the network connection is NOT there     
          }

The final code looks like this:

      NetworkUtil net =new NetworkUtil();
        AdView ad=(AdView)findViewById(R.id.adView);        
        int status = net.getConnectivityStatus(getApplicationContext());
        if(status!=0){
            ad.loadAd(new AdRequest.Builder().build());
           ad.setAdListener(new AdListener() {
                 @Override
                  public void onAdLoaded() {
                           Log.d(TAG, "Ad Received");
                           //code when the ad loaded
                  }
                 @Override
                  public void onAdFailedToLoad(int errorcode) {
                           Log.d(TAG, "Ad Not Loaded Received");
                           //code if the ad loading is failed
                  }
           });            
        }
       else{
                  //Code if the network connection is NOT there                
        }

That’s it. Happy Coding..

No comments: