045 Java Android Program to Monitoring Device State Changes Using Broadcast Intents
Posted by Superadmin on February 26 2017 08:51:01

Java 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

 
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>