59

I want to check the internet connectivity in each activity. If it is lost a message should be displayed.

Can any one guide me how to achieve this?

Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278
  • Try this library that I wrote. https://github.com/muddassir235/connection_checker It provides a listener with onConnected, onConnectionSlow and onDisconnected methods. – Muddassir Ahmed Aug 29 '20 at 00:17

8 Answers8

126

Only one connection can be active at any one point. So a simpler answer is:

final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
    // notify user you are online
} else {
    // notify user you are not online
} 

It also caters for any new type of network such as ConnectivityManager#TYPE_WIMAX


Also make sure that you have the required permission to monitor the network state. You need to add this permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
William
  • 20,150
  • 8
  • 49
  • 91
63

You can use the ConnectivityManager to check the network state.

ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

if ( conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED 
    || conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED ) {

    // notify user you are online

}
else if ( conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.DISCONNECTED 
    || conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.DISCONNECTED) {

    // notify user you are not online
}

Note that the constants ConnectivityManager.TYPE_MOBILE and ConnectivityManager.TYPE_WIFI represent connection types and these two values are not exhaustive. See here for an exhaustive list.


Also make sure that you have the required permission to monitor the network state. You need to add this permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
rsc
  • 10,348
  • 5
  • 39
  • 36
Dan
  • 17,375
  • 3
  • 36
  • 39
10

You can do this, for various types of network status

public void  checkNetworkStatus(){

    final ConnectivityManager connMgr = (ConnectivityManager)
     this.getSystemService(Context.CONNECTIVITY_SERVICE);

     final android.net.NetworkInfo wifi =
     connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

     final android.net.NetworkInfo mobile =
     connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

     if( wifi.isAvailable() ){

     Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
     }
     else if( mobile.isAvailable() ){

     Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
     }
     else
     {

         Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();
     }

}
animuson
  • 53,861
  • 28
  • 137
  • 147
mainu
  • 101
  • 1
  • 2
7

You can check network coverage and data availability of Mobile and wi-fi directly with

For Network coverage availability,

private boolean isNetworkAvailable()
{
 ConnectivityManager conxMgr = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);

 NetworkInfo mobileNwInfo = conxMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
 NetworkInfo wifiNwInfo   = conxMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

return ((mobileNwInfo== null ? false : mobileNwInfo.isAvailable()) || (wifiNwInfo == null ? false : wifiNwInfo.isAvailable()));

}

For Data availability if network available

private boolean isDataAvailable()
{
  ConnectivityManager conxMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

 NetworkInfo mobileNwInfo = conxMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
 NetworkInfo wifiNwInfo   = conxMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

return ((mobileNwInfo== null? false : mobileNwInfo.isConnected() )|| (wifiNwInfo == null? false : wifiNwInfo.isConnected()));
}
Amar Gore
  • 909
  • 8
  • 9
5

Correction

if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
    ||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING  ) {
   //notify user you are online
}

should be

if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
    ||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
    //notify user you are online
}
Raj
  • 22,346
  • 14
  • 99
  • 142
Sudhakar Chavali
  • 809
  • 2
  • 14
  • 32
  • 1
    ok one thing i have noticed.. when wifi is connected to wireless modem internet coverage is there or not it always says yes you are conected it only checks wifi conectivity not internet connectivity so how to handle such a situation ? – UMAR-MOBITSOLUTIONS Nov 19 '10 at 08:10
3

Register a broadcast receiver to handle CONNECTIVITY_ACTION. See this full example. You should update a static variable 'connectionAvailable' that will be accessible everywhere and everytime through its respective getter.

Remember to declare the broadcast receiver in the manifest file:

<receiver android:name=".NetworkConnectivityReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
    </intent-filter>
</receiver>

On the matter of 'checking in each activity', may be you would be interested in using a BaseActivity extended by your activities and managing the test of connectivity and displaying the message.

Also, note that using events (not polling from activities) will be more efficient.

caligari
  • 2,110
  • 20
  • 25
1

You are not using ConnectivityManager.getNetworkInfo(0).getState() and ConnectivityManager.getNetworkInfo(1).getState() properly, instead of hardcoding the values (1) and (0) use ConnectivityManager.TYPE_WIFI and ConnectivityManager.TYPE_MOBILE

Naskov
  • 4,121
  • 5
  • 38
  • 62
0

This is a boolean check to see if you have network access. It doesn't determine what kind of network access - mobile, wifi..., it just checks to see if you're online.

boolean mobileNwInfo = false;  
ConnectivityManager conxMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);  
try { mobileNwInfo = conxMgr.getActiveNetworkInfo().isConnected(); }  
catch (NullPointerException e) { mobileNwInfo = false; }  
if ( mobileNwInfo == false ) {
  // Your code goes here...
}  
VikingGlen
  • 1,705
  • 18
  • 18