How to check your internet connection in Android?

I want to check my internet connection at every event. If it is lost, a message should be displayed.

Can anyone advise me how to do this?

+57
android android-activity network-state
Feb 24 2018-10-24
source share
8 answers

You can use the ConnectivityManager to check the status of the network.

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 ConnectivityManager.TYPE_MOBILE and ConnectivityManager.TYPE_WIFI constants represent connection types, and these two values ​​are not exhaustive. See here for an exhaustive list.




Also make sure that you have the necessary permission to monitor the network status. You must add this permission to your AndroidManifest.xml:

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
+60
Feb 24 2018-10-24
source share

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

 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 is also suitable for any new type of network, such as ConnectivityManager # TYPE_WIMAX




Also make sure that you have the necessary permission to monitor the network status. You must add this permission to your AndroidManifest.xml:

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
+118
Apr 25 '11 at 12:02
source share

This can be done for various types of network conditions.

 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(); } } 
+10
Oct 19 '11 at 7:10
source share

You can directly check network availability and data availability for mobile and wi-fi using

For network 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 is 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())); } 
+6
Mar 08 '13 at 6:28
source share

Correction

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

it should be

 if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED || conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) { //notify user you are online } 
+5
Nov 18 '10 at 11:16
source share

Register the broadcast receiver to the CONNECTIVITY_ACTION descriptor . See This Complete Example . You must update the static variable 'connectionAvailable', which will be available everywhere and every time through the appropriate recipient.

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> 

Regarding β€œchecking every action,” you might be interested in using BaseActivity, enhanced by your actions , and managing the connection check and message display.

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

+3
Sep 05 '13 at 13:34 on
source share

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

+1
Apr 25 '13 at
source share

This is a boolean check to see if you have network access. It does not determine what access to the network is mobile, Wi-Fi ..., it just checks if it is connected.

 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... } 
0
May 29 '13 at 1:41
source share



All Articles