1

Besides this:

InetAddress.getByName("www.xy.com").isReachable(timeout)

or pinging to any server

is there any other way to continuously check if the device has internet access. And by that I mean not just connected to any network, but having actual access to the internet.

Something similar to what lollipop does when it's connected to some WIFI network but the WIFI network does not have internet access and shows an exclamation mark.

Note: I already know the below stuff and the connection change is detected fine

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

if (netInfo != null && netInfo.isConnectedOrConnecting()) {
    isConnected = true;
    networkName = netInfo.getTypeName();
    networkType = netInfo.getType();
}
Vladimir Markeev
  • 654
  • 10
  • 25
patrickfdsouza
  • 747
  • 6
  • 17

2 Answers2

0

There is a broadcast to know the connectivity state. You can get more info in this question

Community
  • 1
  • 1
Arturo
  • 548
  • 6
  • 15
0

To your Manifest File add these :

<receiver android:name=".YOURRECEIVERNAME">
   <intent-filter>
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
   </intent-filter>
</receiver>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Then Create java class like below:

public class YOURRECEIVERNAME extends BroadcastReceiver {
    private static final String TAG = "NetworkStateReceiver";

    @Override
    public void onReceive(final Context context, final Intent intent) {

        Log.d(TAG, "Network connectivity change");

        if (intent.getExtras() != null) {
            final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            final NetworkInfo ni = connectivityManager.getActiveNetworkInfo();

            if (ni != null && ni.isConnectedOrConnecting()) {
                Log.i(TAG, "Network " + ni.getTypeName() + " connected");
            } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
                Log.d(TAG, "There's no network connectivity");
            }
        }
    }
}

Now manually switch on and off network to see effect.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Farid
  • 2,317
  • 1
  • 20
  • 34