DEV Community

Cover image for Your Android App Says “No Internet” With a VPN Connected: Is Your Connectivity Check Wrong?
KlarVPN
KlarVPN

Posted on AI-assisted

Your Android App Says “No Internet” With a VPN Connected: Is Your Connectivity Check Wrong?

Your Android app works over Wi-Fi. You connect a VPN, reopen the app and immediately see “No internet connection.”

Meanwhile, another app loads new content without a problem.

Before changing DNS settings or blaming the VPN server, check whether your app actually attempted a request.

An offline message can come from a faulty connectivity check before any network operation starts. If the app rejects a connection based on its transport type, the error may describe your code’s assumptions rather than the phone’s connectivity.

Here is how to investigate.

1. Find the code that decides the app is offline

Search for the condition that displays the message or prevents the request.

A transport-based check might look like this:

val acceptedTransport =
    capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
    capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
Enter fullscreen mode Exit fullscreen mode

That tells you whether those transports are reported. It does not establish whether your API is reachable.

Android networks can also report Ethernet or VPN transport. A network can have multiple transports at once, including a VPN and its underlying connection.

That means a Wi-Fi-or-cellular check will not necessarily fail for every VPN. The broader problem is using a transport list as an internet-access test.

Also look for an explicit VPN rejection:

val consideredOnline =
    capabilities.hasCapability(
        NetworkCapabilities.NET_CAPABILITY_INTERNET
    ) &&
    !capabilities.hasTransport(
        NetworkCapabilities.TRANSPORT_VPN
    )
Enter fullscreen mode Exit fullscreen mode

This labels a VPN connection offline regardless of whether requests would succeed.

If your application intentionally restricts VPN use, that is a separate product policy. “No internet connection” is still an inaccurate explanation of that decision.

2. Separate transport, validation and service reachability

These observations answer different questions:

Observation What it tells you
Network transport How Android describes the connection
NET_CAPABILITY_INTERNET The network is configured for internet access
NET_CAPABILITY_VALIDATED Android’s probes have validated public internet access
Successful API request That particular operation reached a service and received a response

INTERNET is not proof of working connectivity. A network behind a captive portal can have that capability.

VALIDATED provides stronger evidence, but it does not guarantee access to your backend. Your endpoint could still fail because of DNS, TLS, filtering or a server problem.

The reverse deserves care too: an unvalidated network may still reach a particular private service.

Use these signals to explain the network state. Keep the actual request result separate.

3. Trace changes to the app’s default network

For an app using ordinary default routing, observe the default network rather than searching for any available Wi-Fi connection.

The following diagnostic class logs network events and selected capabilities. It targets Android 7.0/API 24 or later.

Add these permissions as children of the manifest element:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
Enter fullscreen mode Exit fullscreen mode

Then add the diagnostic observer:

import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.util.Log

class DefaultNetworkTrace(context: Context) {
    private val manager =
        context.applicationContext.getSystemService(
            ConnectivityManager::class.java
        )

    private var started = false

    private val callback =
        object : ConnectivityManager.NetworkCallback() {

            override fun onAvailable(network: Network) {
                Log.d(TAG, "Default network available: $network")
            }

            override fun onCapabilitiesChanged(
                network: Network,
                capabilities: NetworkCapabilities
            ) {
                val internet = capabilities.hasCapability(
                    NetworkCapabilities.NET_CAPABILITY_INTERNET
                )
                val validated = capabilities.hasCapability(
                    NetworkCapabilities.NET_CAPABILITY_VALIDATED
                )
                val vpn = capabilities.hasTransport(
                    NetworkCapabilities.TRANSPORT_VPN
                )

                Log.d(
                    TAG,
                    "network=$network " +
                        "internet=$internet " +
                        "validated=$validated vpn=$vpn"
                )
            }

            override fun onLost(network: Network) {
                Log.d(TAG, "Default network lost: $network")
            }
        }

    // Call start and stop from the same lifecycle thread.
    fun start() {
        if (started) return
        manager.registerDefaultNetworkCallback(callback)
        started = true
    }

    fun stop() {
        if (!started) return
        manager.unregisterNetworkCallback(callback)
        started = false
    }

    private companion object {
        const val TAG = "DefaultNetworkTrace"
    }
}
Enter fullscreen mode Exit fullscreen mode

Keep one observer instance for the lifecycle you are inspecting. Register it when observation starts and unregister it when it ends.

This class records evidence; it does not implement a production connectivity state holder.

Notice that onAvailable() does not immediately fetch capabilities. Android’s guidance recommends using the information delivered to onCapabilitiesChanged() rather than making synchronous queries inside the callback that can race with network updates.

Callbacks also arrive asynchronously. No callback yet should not automatically become a permanent “offline” state.

4. Interpret network loss in context

For a default-network callback, onLost() means that network no longer satisfies the default-network tracking relationship. It does not mean every connection on the device has disappeared.

This matters when a VPN connects, disconnects or changes its underlying network.

Record the network identifier and event order. If your app maintains state, ensure an event associated with an older network cannot erase newer state.

Avoid updating the UI directly from callback methods. Deliver state through your application’s chosen thread-safe mechanism and render it on the main thread.

Also check how your HTTP client routes requests. Explicit process binding, network-bound sockets or a custom client configuration can make its route differ from the default network you are observing.

5. Inspect custom network filters

If your app uses registerNetworkCallback() with a custom NetworkRequest, inspect the request itself.

Android includes NET_CAPABILITY_NOT_VPN among the default capabilities of a new network request. A filter that retains this capability excludes VPN networks from its matches.

For a deliberate observer of internet-capable networks that should include VPNs, the request can remove that exclusion:

val request = NetworkRequest.Builder()
    .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
    .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
    .build()
Enter fullscreen mode Exit fullscreen mode

This requires the android.net.NetworkRequest import.

Removing the exclusion allows VPN networks to match; it does not restrict the request to VPNs.

A matching-network observer can report multiple networks. Finding one working network does not establish that your application’s requests use it. For default-routed requests, the default-network callback is usually the clearer diagnostic starting point.

6. Let the request produce evidence

A connectivity precheck can become stale before the next line of code runs.

For a user-triggered refresh, a useful design is to attempt the operation through the normal HTTP client, use an appropriate timeout and handle its outcome.

Keep these results distinct:

  • The request was never attempted because a local guard rejected it.
  • Hostname resolution failed.
  • Connection establishment failed.
  • TLS negotiation failed.
  • The server returned an HTTP error.
  • The response arrived but could not be processed.

Do not collapse an HTTP 401 or 403 into “No internet.” An HTTP response means a server or intermediary responded; the next investigation concerns that response.

Similarly, a generic public website loading does not prove that your application’s authenticated endpoint works.

For diagnostic requests, use an endpoint you control and correlate client timestamps with server logs. Avoid recording tokens, private payloads or full URLs containing sensitive parameters.

7. Reproduce the issue with controlled comparisons

Keep the phone, app build, account and API operation unchanged.

Test these four conditions:

Test Underlying network VPN
A Wi-Fi Disconnected
B Same Wi-Fi Connected
C Cellular Disconnected
D Same cellular connection Connected

For the VPN for Android used in the comparison, record its version, selected location and any app exclusions.

In each state, capture:

  • Default-network events.
  • Reported capabilities.
  • Whether the app attempted the request.
  • Request duration.
  • HTTP status or exception category.
  • The message displayed to the user.

Then test connecting the VPN while the app remains open. A bug that appears only during transitions may involve stale state or callback handling rather than the initial capability check.

Use non-sensitive test traffic for disconnected baselines.

8. Match the fix to the finding

If the app blocks requests because it rejects VPN transport, correct that guard.

If requests succeed while the UI remains offline, inspect the state updates and rendering path.

If Android reports validated connectivity but the request fails, investigate the endpoint, DNS, TLS and server response.

If the issue appears only when switching networks, inspect event ordering and how the HTTP client recovers.

Adding TRANSPORT_VPN to a Wi-Fi-or-cellular condition may make one reproduction pass. It still leaves transport type standing in for service reachability.

A stronger design uses network capabilities as context and treats request outcomes as evidence about the operation the user actually needs.

Before closing the bug

Verify two things independently:

  1. The app attempts an appropriate request over the intended connection.
  2. The UI accurately describes the result.

“Your session expired,” “The server could not be reached” and “Waiting for a connection” describe different situations.

The goal is to remove the incorrect assumption that produced “No internet”—and preserve enough information to diagnose a real failure when one occurs.

Top comments (1)

Collapse
 
yonto profile image
Yonto Player •

Seen this on Android TV boxes too, where a lot of people run a VPN app all the time. Treating the network as fine until a real request fails, and only then showing an offline state, is more reliable than reading capabilities. NET_CAPABILITY_VALIDATED is a hint, not a verdict, especially on boxes whose vendor changed the captive portal check.