7

I want to know how to trigger BroadcastReceiver if I turn on/off mobile cellular data. I already registered BroadcastReceiver and it is working fine if I turn on/off wifi but if I turn on/off cellular data no broadcast trigger. can anyone please help me about this?

Here is my code.

Here I register BroadcastReceiver in Manifest.file

       <receiver android:name="com.servicesandroid.NetworkCheckReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

Manifest permissions

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

Here is my BroadcastReceiver class.

public class NetworkCheckReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "Started", Toast.LENGTH_SHORT).show();
         Log.d("app","Network connectivity change");
    }

}
Chintan Bawa
  • 159
  • 2
  • 6

2 Answers2

11

You need to specify appropriate permissions and do the needful as mentioned below:

Permissions in manifest:

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

Receiver declaration in manifest:

        <receiver
        android:name=".NetworkCheckReceiver"
        >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

NetworkCheckReceiver class file:

public class NetworkCheckReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            Log.d("NetworkCheckReceiver", "NetworkCheckReceiver invoked...");


            boolean noConnectivity = intent.getBooleanExtra(
                    ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

            if (!noConnectivity) {
                Log.d("NetworkCheckReceiver", "connected");
            }
            else
            {
                Log.d("NetworkCheckReceiver", "disconnected");
            }
        }
    }

}

Note:

Make sure you have working mobile cellular data connection. This receiver won't trigger if you don't have internet pack and you just switching on/off. You surely need to have working connection to test.

Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
  • Yes, its working now. My cellular data plan was not active thats why it wasn't working. thanks @Mehul – Chintan Bawa Dec 19 '14 at 06:15
  • Following two permissions are sufficient` ` if you will give `` too `BroadcastReceiver` `onReceive` method call two times when you turn on/off wifi connection. – Chintan Bawa Dec 19 '14 at 06:20
  • @ChintanBawa: I'm glad to help. you can either skip that permission or you can do something as mentioned [here](http://stackoverflow.com/a/5890104/1288725) to checkout further about connection type. i.e. mobile,wifi – Mehul Joisar Dec 19 '14 at 06:37
  • @MehulJoisar Can you share code of..How to get result form broadcast receiver to my acitivity..... – Gowthaman M Feb 19 '18 at 14:14
  • @GowthamanM You may use LocalBroadcastManager to broadcast from onReceive of NetworkCheckReceiver and to receive on your Activity. – Mehul Joisar Feb 21 '18 at 11:09
  • @MehulJoisar I solved my pbm sir.....Thank you so much for your responce – Gowthaman M Feb 21 '18 at 11:27
2

Try the following this will receives when the mobile data and wifi changes

 public class NetworkCheckReceiver extends BroadcastReceiver {

        /*
         * (non-Javadoc)
         * 
         * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
         * android.content.Intent)
         */
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("NetworkCheckReceiver","ConnectionChangeReceiver.onReceive()");
            String statusString = getConnectivityStatusString(context);
            Toast.makeText(context, statusString, Toast.LENGTH_SHORT).show();
        }

        public int TYPE_WIFI = 1;
        public int TYPE_MOBILE = 2;
        public int TYPE_NOT_CONNECTED = 0;

        public 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 TYPE_WIFI;

                if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                    return TYPE_MOBILE;
            }
            return TYPE_NOT_CONNECTED;
        }

        public String getConnectivityStatusString(Context context) {
            int conn = getConnectivityStatus(context);
            String status = null;
            if (conn == TYPE_WIFI) {
                status = "Wifi enabled";
            } else if (conn == TYPE_MOBILE) {
                status = "Mobile data enabled";
            } else if (conn == TYPE_NOT_CONNECTED) {
                status = "Not connected to Internet";
            }
            return status;
        }

    }
hareesh J
  • 520
  • 1
  • 6
  • 21
  • I already given permissions. Sorry, I forgot to mention them. – Chintan Bawa Dec 19 '14 at 04:59
  • I tried this code before and try it now also but its just broadcast for wifi not for mobile cellular data. Did u tried this code. – Chintan Bawa Dec 19 '14 at 05:20
  • yes it is working for both mobile data as well as wifi and i updated the code by adding log messages once the mobile data and wifi enabled or disabled it will receives check the log messages @Chintan – hareesh J Dec 19 '14 at 05:30
  • My cellular data plan was not active thats why it wasn't working. thanx @hareesh – Chintan Bawa Dec 19 '14 at 06:18