I’m trying to develop an app which first checks if the device is connected to web or not.
If yes, then certain activity is started else other activity will start.
ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
boolean isConnected = ni.isConnected();
if (isConnected) {
//connected, do something online.
} else {
//Not connected.
}
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.
Note: I have added the activities in the manifest file and also have declared following user-permissions:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Can anyone please help me with this problem and point in the right direction?
Try this:
Add permission in the manifest: