Check Internet Connection status using Broadcast Receiver in Android
Android Sample Codes

Check Internet Connection status using Broadcast Receiver in Android

Check Internet Connection status using Broadcast Receiver in Android

Check Internet Connection status using Broadcast Receiver in Android allows you to check the network connectivity information of the device. That is whether our mobile is connected to mobile data or wifi data for accessing the internet.

It is very important to check the internet connection of the device while performing the task. That is which data connection is used to access the information from the server.

We can determine the types of a network of android device are used to connect to the internet. It may be of types TYPE_WIFI (wifi), TYPE_MOBILE (mobile).

Check Internet Connection status using Broadcast Receiver in Android Example

Check Internet Connection status using Broadcast Receiver in Android tutorial is used to check the network connectivity of the device as well as its type.

To access the network connectivity of a device, we need to provide the network access permission in AndroidMenifest.xml file.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />

In the activity_main.xml file of layout add the following code.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

The code for activity class file MainActivity.java is as follows

MainActivity.java

package com.example.networkconnectioncheckusingbroadcastreceiver;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Create a new java class named as NetworkChangeReceive.java extends the BroadcastReciever class. This class handles the changes occur in the network state of a device. The onReceive() method of BroadcastReciever class called when the network state of device changes.

NetworkChangeReceive.java

package com.example.networkconnectioncheckusingbroadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class NetworkChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null) {
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
               Toast.makeText(context, "Wifi enabled", Toast.LENGTH_LONG).show();

            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                Toast.makeText(context, "Mobile data enabled", Toast.LENGTH_LONG).show();

            }
        } else {
            Toast.makeText(context, "No internet is available", Toast.LENGTH_LONG).show();

        }
    }
}

The code context.getSystemService(Context.CONNECTIVITY_SERVICE) is used to return the object of the ConnectivityManager class which is used to access the network properties of you phone.

The method getActiveNetworkInfo() returns a NetworkInfo object which is used to check the internet connection is available or not.

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
    if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
       Toast.makeText(context, "Wifi enabled", Toast.LENGTH_LONG).show();
    } 
    else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
        Toast.makeText(context, "Mobile data enabled", Toast.LENGTH_LONG).show();
    }
}
 else {
    Toast.makeText(context, "No internet is available", Toast.LENGTH_LONG).show();
}

activeNetwork.getType() method return the type of network (mobile data or wifi data) you are using presently. Compare it with ConnectivityManager.TYPE_WIFI (wifi) and ConnectivityManager.TYPE_MOBILE (mobile ) for displaying the toast.

In the AndroidMenifest.xml file, add the network access permission and receiver class that handles the changes occur in BroadcastReceiver class.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.networkconnectioncheckusingbroadcastreceiver">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".NetworkChangeReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

Output :

When you enable either Mobile network or wifi network, a toast will be displayed as shown below.

Check Internet Connection status using Broadcast Receiver in Android

 

 

Check Internet Connection status using Broadcast Receiver in Android

 

 

 

Check Internet Connection status using Broadcast Receiver in Android

Check Internet Connection status using Broadcast Receiver in Android

Leave a Reply

Your email address will not be published.