Check Internet Connection


this is the simplest way i can find that you can check for an internet connection.

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo Wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo Network = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if (Wifi.isConnected()) {
Toast msg1 = Toast.makeText(advanced.this,
“Wifi is connected”, Toast.LENGTH_SHORT);
msg1.show();
} else if {Network.isConnected()) {
Toast msg1 = Toast.makeText(advanced.this,
“Mobile data is connected”, Toast.LENGTH_SHORT);
msg1.show();
} else {
Toast msg1 = Toast.makeText(advanced.this,
“No connection to the internet available”, Toast.LENGTH_SHORT);
msg1.show();
}

and the permission for the manafest need to include these (First one, only if you want to use the internet)

<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE” />
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” />

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: