I have an app which updates the database whenever the phone is connected to WiFi. I have implemented a Service
and BroadcastReceiver
which will run the Service
(it will tell me what network is in use), but the problem is I don't know what to add in the manifest
file to start BroadcastReceiver
when the network state changes or when it connects to some kind of network

- 237,138
- 77
- 654
- 440

- 920
- 3
- 12
- 25
5 Answers
You need
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
In your receiver
tag.
Or if you want more control over it, before registering BroadcastReceiver
set these up:
final IntentFilter filters = new IntentFilter();
filters.addAction("android.net.wifi.WIFI_STATE_CHANGED");
filters.addAction("android.net.wifi.STATE_CHANGE");
super.registerReceiver(yourReceiver, filters);
WIFI_STATE_CHANGED
Broadcast <intent-action>
indicating that Wi-Fi has been enabled, disabled, enabling, disabling, or unknown. One extra provides this state as an int. Another extra provides the previous state, if available.
STATE_CHANGE
Broadcast <intent-action>
indicating that the state of Wi-Fi connectivity has changed. One extra provides the new state in the form of a NetworkInfo object. If the new state is CONNECTED, additional extras may provide the BSSID and WifiInfo of the access point. as a String
Also, you'll need to specify the right permissions inside manifest
tag:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
To check connectivity, you can use ConnectivityManager
as it tells you what type of connection is available.
ConnectivityManager conMngr = (ConnectivityManager)this.getSystemService(this.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = conMngr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = conMngr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

- 237,138
- 77
- 654
- 440

- 9,162
- 1
- 31
- 42
-
1Can we also add a broadcast receiver to check data connection? – tasomaniac Nov 19 '12 at 20:04
-
2I think this only give us the connection changes in WIFI adapter. I want to be notified when the mobile data connection has changed. – tasomaniac Nov 20 '12 at 09:44
-
4Thanks iccthedral. As an alternative to the string literals passed to the filters, WifiManager.WIFI_STATE_CHANGED_ACTION and WifiManager.NETWORK_STATE_CHANGED_ACTION are the equivalent definitions in WifiManager.class. – pmont Aug 11 '13 at 18:01
This is what I do to get notified when connection has changed.
You define a BroadCastReceiver
to receive the broadcast.
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isConnected = wifi != null && wifi.isConnectedOrConnecting() ||
mobile != null && mobile.isConnectedOrConnecting();
if (isConnected) {
Log.d("Network Available ", "YES");
} else {
Log.d("Network Available ", "NO");
}
}
}
You also have to define that BroadcastReceiver
in your manifest file and filter by ConnectivityChange
.
<receiver android:name=".NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Source here

- 237,138
- 77
- 654
- 440

- 561
- 6
- 15
-
Please, consider that starting with Android N the `android.net.conn.CONNECTIVITY_CHANGE"` broadcast is deprecated. The documentation suggests to use `JobScheduler` or `GCMNetworkManager` – Nicolás Carrasco-Stevenson Sep 21 '17 at 15:55
-
3@Nicholas it's not deprecated. You just have to create the broadcast receiver within the code, not in the manifest file. – Mr.Noob Dec 01 '17 at 07:27
To complement @Dantalian's answer. Starting with Android Nougat you should not declare your receiver on your Manifest because it will not receive the CONNECTIVITY_ACTION
broadcast. You should instead register your receiver using the Context.registerReceiver(Receiver, Intent)
method.
Link to the source here

- 3,430
- 2
- 26
- 41
Declare the intent filter to pass to the broadcastreceiver
IntentFilter filter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
registerReceiver(mReceiver, filter);
In the broadcast receiver, check for EXTRA_WIFI_STATE;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action)
{
case WifiManager.WIFI_STATE_CHANGED_ACTION:
int extra = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,0);
if(extra==WifiManager.WIFI_STATE_ENABLED)
{
}//...else WIFI_STATE_DISABLED, WIFI_STATE_DISABLING, WIFI_STATE_ENABLING
break;
}
}
};

- 47
- 3
You should make an BroadcastReceiver
that will be triggered when the connectivity status has changed :
Here Same question How to check the Internet Connection periodically in whole application?

- 1
- 1

- 35,760
- 13
- 86
- 95