checking whether device is connected to web
<p>I'm trying to develop an app which first checks if the device is connected to web or not.</p>

<p>If yes, then certain activity is started else other activity will start.</p>

<pre><code>ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
boolean isConnected = ni.isConnected();

if (isConnected) {
    //connected, do something online.
} else {
    //Not connected.  
}
</code></pre>

<p>I have written the above code as an event when the particular button is pressed. But when i run it on the emulator and press that button, i'm getting a force close message. </p>

<p><strong>Note:</strong> I have added the activities in the manifest file and also have declared following user-permissions:</p>

<pre><code>&lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
</code></pre>

<p>Can anyone please help me with this problem and point in the right direction?</p>
