Difficulty in hiding ad when the internet connection is not available
<p>I am making an application in which i am providing an ad.I want to hide the ad when the internet connection is not available.But i was not able to do that.This is the code which i used to achieve that result.Please tell me what is the problem with the code.</p>

<pre><code>AdView myAddView = (AdView) findViewById(R.id.ad);
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            myAddView.setVisibility(8);
        } 
</code></pre>
