Android Program to Monitoring Device State Changes using Broadcast Intents

Here is source code of the Program to Monitor Device State Changes Using Broadcast Intents. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

To monitor changes in connectivity, register a Broadcast Receiver (either within your application or within the manifest) to listen for the (ConnectivityManager.CONNECTIVITY_ACTION) action.

This source code detects whether there is an Internet connection or not and displays a toast accordingly.

package com.example.monitor_device_change_intent;
import android.content.Context;
public class NetCheck {
    public static int WIFI = 1;
    public static int MOBILE = 2;
    public static int NOT_CONNECTED = 0;
    public static int getConnectivityStatus(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (null != activeNetwork) {
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                return WIFI;
            if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return MOBILE;
        return NOT_CONNECTED;
    public static String getConnectivityStatusString(Context context) {
        int conn = NetCheck.getConnectivityStatus(context);
        String status = null;
        if (conn == NetCheck.WIFI) {
            status = "Wifi enabled";
        } else if (conn == NetCheck.MOBILE) {
            status = "Mobile data enabled";
        } else if (conn == NetCheck.NOT_CONNECTED) {
            status = "Not connected to Internet";
        return status;


package com.example.monitor_device_change_intent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
    public void onReceive(final Context context, final Intent intent) {
        String status = NetCheck.getConnectivityStatusString(context);
        Toast.makeText(context, status, Toast.LENGTH_LONG).show();


<manifest xmlns:android=""
    android:versionName="1.0" >
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        android:theme="@style/AppTheme" >
            android:label="NetChangeReceiver" >
                <action android:name="" />
                <action android:name="" />


Note: Join free Sanfoundry classes at Telegram or Youtube

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.