This is android check internet connection tutorial.
When we perform any network related operation like making HTTP request, then it is good to check internet or network connectivity. A device can have different types of networks like Wi-Fi or mobile network.
In android we can check internet connectivity using ConnectivityManager class. First we need to instantiate an object of ConnectivityManager class by calling getSystemService() method.
After this we can get information of all networks using getAllNetworkInfo() method. This method will return an array of NetworkInfo type.
Finally we will check connection status and show a message to user.
Below example will show you how to check internet connectivity in android
Android Check Internet Connection
To check network connectivity we have to add ACCESS_NETWORK_STATE permission in AndroidManifest.xml file.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="thecrazyprogrammer.androidexample" > <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check Internet Connectivity" android:id="@+id/button" android:onClick="buttonAction"/> </LinearLayout>
MainActivity.java
package thecrazyprogrammer.androidexample; import android.app.Activity; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button); } public void buttonAction(View view) { //instantiate an object ConnectivityManager cm=(ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); //get all networks information NetworkInfo networkInfo[]=cm.getAllNetworkInfo(); int i; //checking internet connectivity for(i=0;i<networkInfo.length;++i){ if(networkInfo[i].getState()==NetworkInfo.State.CONNECTED){ Toast.makeText(getApplicationContext(),"Internet Connected",Toast.LENGTH_LONG).show(); break; } } if(i==networkInfo.length){ Toast.makeText(getApplicationContext(),"Internet Not Connected",Toast.LENGTH_LONG).show(); } } }
When Internet Connected
When Internet Not Connected
Comment below if you are facing any problem related to above android check internet connection tutorial.
Happy Coding!! 🙂 🙂
What if we are connected to the router but have no Internet access ? Or if we are connected to one of hotspots where we need to login first ?
Then that condition will be treated as Internet Connected.