Home » Android » Android Detect Internet Connection Status

Android Detect Internet Connection Status

Detecting internet connection status in your app is very easy and won’t take more than 5mins. In this article you will learn how to detect internet connection status manually and automatically. Using broadcast receiver, your app will be automatically notified when there is a change in network connection.

This provides easy way do any changes in the app like hiding few button (like WhatsApp hides send, upload icon when there is not internet), navigation user to another screen, or just show a Snackbar message.

Creating New Project

1. Create a new project in Android Studio from File ⇒ New Project. When it prompts you to select the default activity, select Blank Activity and proceed.

2. Create a class named ConnectivityReceiver.java and extend it from BroadcastReceiver. This is a receiver class which will be notified whenever there is change in network / internet connection.

package info.androidhive.checkinternet;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectivityReceiver
        extends BroadcastReceiver {

    public static ConnectivityReceiverListener connectivityReceiverListener;

    public ConnectivityReceiver() {

    public void onReceive(Context context, Intent arg1) {
        ConnectivityManager cm = (ConnectivityManager) context
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null
                && activeNetwork.isConnectedOrConnecting();

        if (connectivityReceiverListener != null) {

    public static boolean isConnected() {
                cm = (ConnectivityManager) MyApplication.getInstance().getApplicationContext()
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null
                && activeNetwork.isConnectedOrConnecting();

    public interface ConnectivityReceiverListener {
        void onNetworkConnectionChanged(boolean isConnected);

3. Create another class named MyApplication.java and extend it from Application. This class will be called whenever app is launched. Here setConnectivityListener() method is used to initiate the connectivity listener.

package info.androidhive.checkinternet;

import android.app.Application;

public class MyApplication extends Application {

    private static MyApplication mInstance;

    public void onCreate() {

        mInstance = this;

    public static synchronized MyApplication getInstance() {
        return mInstance;

    public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) {
        ConnectivityReceiver.connectivityReceiverListener = listener;

4. Open AndroidManifest.xml and do the below changes.

> Add MyApplication to tag.

> Add ConnectivityReceiver as .

> Declare INTERNET and ACCESS_NETWORK_STATE permissions.






Broadcasting Internet Status to All Activities

Now we have all the setup ready. Let’s see how to notify an activity when the device is connected or disconnected from internet.

5. Open layout file main activity activity_main.xml add the below layout.








6. Open your MainActivity.java and do the below changes to receive the internet status.

> Register the connection change listener in onResume() method by calling MyApplication.getInstance().setConnectivityListener(this).

> Implement the activity from ConnectivityReceiver.ConnectivityReceiverListener which will override onNetworkConnectionChanged() method.

> onNetworkConnectionChanged() method will be triggered whenever device is connected / disconnected from internet. You need to take appropriate action here.

Follow the above same three steps in all other activities in which in you want to notify the internet status.

package info.androidhive.checkinternet;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
        implements ConnectivityReceiver.ConnectivityReceiverListener {

    private Button btnCheck;

    protected void onCreate(Bundle savedInstanceState) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        btnCheck = (Button) findViewById(R.id.btn_check);

        // Manually checking internet connection

        btnCheck.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Manually checking internet connection

    // Method to manually check connection status
    private void checkConnection() {
        boolean isConnected = ConnectivityReceiver.isConnected();

    // Showing the status in Snackbar
    private void showSnack(boolean isConnected) {
        String message;
        int color;
        if (isConnected) {
            message = "Good! Connected to Internet";
            color = Color.WHITE;
        } else {
            message = "Sorry! Not connected to internet";
            color = Color.RED;

        Snackbar snackbar = Snackbar
                .make(findViewById(R.id.fab), message, Snackbar.LENGTH_LONG);

        View sbView = snackbar.getView();
        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);

    protected void onResume() {

        // register connection status listener

     * Callback will be triggered when there is change in
     * network connection
    public void onNetworkConnectionChanged(boolean isConnected) {

Run the project and try turning off / on wifi or mobile data. You can notice a Snackbar is shown with network connection status.

Hi there! I am Founder at androidhive and programming enthusiast. My skills includes Android, iOS, PHP, Ruby on Rails and lot more. If you have any idea that you would want me to develop? Let’s talk: ravi@androidhive.info

Leave a Reply

Your email address will not be published. Required fields are marked *