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 android.net.conn.CONNECTIVITY_CHANGE (ConnectivityManager.CONNECTIVITY_ACTION) action.

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

NetCheck.java

package com.example.monitor_device_change_intent;
 
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
 
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
                .getSystemService(Context.CONNECTIVITY_SERVICE);
 
        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;
    }
}

MyReceiver

advertisement
advertisement
 
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 {
 
    @Override
    public void onReceive(final Context context, final Intent intent) {
 
        String status = NetCheck.getConnectivityStatusString(context);
 
        Toast.makeText(context, status, Toast.LENGTH_LONG).show();
    }
}

AndroidManifest

 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.monitor_device_change_intent"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
            android:name="com.example.monitor_device_change_intent.MyReceiver"
            android:label="NetChangeReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>
 
</manifest>

Capture

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.

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

advertisement
advertisement
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.