Check Internet Connection Code Snippet For Android Studio
Welcome. In this tutorial you will learn how to check for Active Internet Connection in your Android Devices.
Android Internet Connectivity Check Example
When we are creating android apps that require internet access, then it is a good habit to first check for Internet Access. So to check whether your Android device has an active Internet Connection or not, you can use this code snippet given below.
1 2 3 4 5 6 7 8 9 10 |
protected boolean isOnline() { ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { return true; } else { return false; } } |
We will have to use ConnectivityManager for this purpose. Connectivity manager is used to manage and monitor active connections (WIFI, Mobile Data, etc) and send broadcast intents when network connectivity changes. It has various functions for several different purposes. The function getActiveNetworkInfo returns details about the currently active default data network. One should always check networkInfo isConnected before initiating any kind of network traffic. This may return a null value when there is no network available.However you should note that using this function is deprecated in API level 29, so take that in mind before using this for creating apps for API level 29 or above.
The above code stated here will return true if the device has active internet connection else it will return false. You can use this function anywhere while creating your app. Just call the above function from anywhere and check for true or false.
Also note that the above code will require Internet so you will have to first modify the AndroidManifests file and add the following code to it:
<uses-permission android:name="android.permission.INTERNET" />
The image below gives an example for checking internet connection in Android When the button is pressed, it will check for internet and display it as Toast.
The Complete Code Snippet for above example is given below:-
Java (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.tutorial.personal.androidstudiopro.R; public class internetstatus extends AppCompatActivity { private Button checkIntrnetButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_internetstatus); checkIntrnetButton = (Button)findViewById(R.id.checkInternet); checkIntrnetButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkConnection(); } }); } protected boolean isOnline() { ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { return true; } else { return false; } } public void checkConnection(){ if(isOnline()){ Toast.makeText(internetstatus.this, "You are connected to Internet", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(internetstatus.this, "You are not connected to Internet", Toast.LENGTH_SHORT).show(); } } } |
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/checkInternet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="10dp" android:paddingLeft="10dp" android:text="check Internet Connection"/> </LinearLayout> |
You can directly copy paste these snippets to your android studio, and they should work perfectly. Just save and run your project.
For a more detailed guide, you can also visit android’s official guide below:-
https://developer.android.com/reference/android/net/ConnectivityManager
Comment down below if you are facing any problems.