
Editorial illustration.
Originally published 26 June 2023; rewritten and reviewed on 15 October 2026.
A user starts uploading a photo or syncing an order on office Wi-Fi, walks out to the car park, and the phone hands off to 4G/5G mid-request. For a field-service or delivery app, detecting that switch from Wi-Fi to mobile data is not just about updating a status icon—it decides whether your app pauses a heavy upload, warns the user about mobile data, or safely retries an interrupted HTTP call.
Older Android tutorials tell you to register a BroadcastReceiver for ConnectivityManager.CONNECTIVITY_ACTION and read NetworkInfo. Both NetworkInfo and background implicit broadcasts have been deprecated for years. On modern Android (API 24+), ConnectivityManager.NetworkCallback is the reliable way to observe network transitions in Java.
Registering a NetworkCallback in Java
First, make sure your AndroidManifest.xml declares the permission (it is a normal permission, so no runtime prompt is needed):
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
In your Activity, Fragment, or connectivity repository, obtain ConnectivityManager and register a default network callback:
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import androidx.annotation.NonNull;
public class NetworkMonitor {
private final ConnectivityManager connectivityManager;
private ConnectivityManager.NetworkCallback networkCallback;
public NetworkMonitor(Context context) {
this.connectivityManager = (ConnectivityManager)
context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
}
public void start() {
if (connectivityManager == null || networkCallback != null) {
return;
}
networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onCapabilitiesChanged(
@NonNull Network network,
@NonNull NetworkCapabilities capabilities) {
boolean isWifi = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
boolean isCellular = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR);
boolean isValidated = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
// Dispatch to main thread before touching any Views
}
@Override
public void onLost(@NonNull Network network) {
// Default network dropped; check if another network takes over
}
};
connectivityManager.registerDefaultNetworkCallback(networkCallback);
}
public void stop() {
if (connectivityManager != null && networkCallback != null) {
try {
connectivityManager.unregisterNetworkCallback(networkCallback);
} catch (IllegalArgumentException ignored) {
// Guard against unregistering an already-released callback
} finally {
networkCallback = null;
}
}
}
}
If you need to support API levels below 24 (Build.VERSION_CODES.N), use registerNetworkCallback(new NetworkRequest.Builder().build(), networkCallback) instead of registerDefaultNetworkCallback.
Transport type does not guarantee working internet
Two traps catch developers off guard when testing network switches on real phones:
- Captive portals and dead Wi-Fi: Walking into a cafe or mall often connects the phone to Wi-Fi before the user taps through the login portal.
TRANSPORT_WIFIistrue, andNET_CAPABILITY_INTERNETistrue, yet every API call times out. CheckingNetworkCapabilities.NET_CAPABILITY_VALIDATEDtells you whether Android’s own connectivity probe actually reached the internet. - VPNs and dual-SIM handoffs: A user running a VPN or switching between SIM 1 and SIM 2 can trigger
onCapabilitiesChangedmultiple times in a second, or reportTRANSPORT_VPNalongsideTRANSPORT_CELLULAR. Do not treat Wi-Fi and cellular as mutually exclusive booleans without checkingNET_CAPABILITY_VALIDATED.
Even with NET_CAPABILITY_VALIDATED, an active TCP socket opened on Wi-Fi will still break when the IP address changes to mobile data. If your app uploads files or submits payments, make the endpoint idempotent (with a client-generated request ID) so retrying after a handoff never creates duplicate records.
Lifecycle and threading rules
NetworkCallback methods run on Android’s ConnectivityThread, not the main UI thread. Updating a TextView or Snackbar directly inside onCapabilitiesChanged or onLost will crash with CalledFromWrongThreadException. Always post UI updates back to the main thread via Handler(Looper.getMainLooper()).post(...) or LiveData.postValue(...).
Finally, pair start() and stop() cleanly with your lifecycle (onStart() / onStop() in an Activity, or Application scope if the monitor is shared). Calling registerDefaultNetworkCallback repeatedly without unregistering leaks callbacks and will eventually throw TooManyRequestsException once an app exceeds Android’s per-UID callback limit.