25

I am working on an Android app that will continuously remain connected to Internet. If Internet is dow, it should give an appropriate message to the User.

Is there any thing like Internet Listener? Or how to implement this event that whenever Internet connection is not available it should give alert.

Bentaye
  • 8,857
  • 4
  • 30
  • 41
Zeeshan Chaudhry
  • 842
  • 2
  • 15
  • 31
  • This other Q&A could help: http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts – helios Aug 28 '12 at 10:17
  • http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html – Muhammad Babar Nov 25 '14 at 07:18

2 Answers2

50

Create one Broadcast Receiver for that and register it in manifest file.

First create a new class NetworkStateReceiver and extend BroadcastReceiver.

public class NetworkStateReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
     Log.d("app","Network connectivity change");
     if(intent.getExtras()!=null) {
        NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
        if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
            Log.i("app","Network "+ni.getTypeName()+" connected");
        }
     }
     if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
            Log.d("app","There's no network connectivity");
     }
   }
}

Put this code in your AndroidManifest.xml under the "application" element:

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

And add this permission

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

EDIT

This code just detects connectivity change but cannot tell whether the network it is connected to has a internet access. Use this method to check that -

public static boolean hasActiveInternetConnection(Context context) {
    if (isNetworkAvailable(context)) {
        try {
            HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
            urlc.setRequestProperty("User-Agent", "Test");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(1500); 
            urlc.connect();
            return (urlc.getResponseCode() == 200);
        } catch (IOException e) {
        Log.e(LOG_TAG, "Error checking internet connection", e);
        }
    } else {
    Log.d(LOG_TAG, "No network available!");
    }
    return false;
}
Confuse
  • 5,336
  • 6
  • 33
  • 57
Chirag
  • 56,384
  • 29
  • 154
  • 197
  • Samples here: http://stackoverflow.com/questions/6176570/checking-the-networking-connectivity-using-broadcastreceiver-in-android – helios Aug 28 '12 at 10:20
  • I have tried this solution but this not working dude. also gives error at super.onReceive(context, intent); – Zeeshan Chaudhry Aug 28 '12 at 10:27
  • cannot directly invoke abstract methord onReceive(context, intent) – Zeeshan Chaudhry Aug 28 '12 at 10:32
  • Just Create BroadCast Receiver and write the above code and delcare it in manifest file . nothing else – Chirag Aug 28 '12 at 10:33
  • i have created a seperate class NetworkStateReceiver and write the above code there also add the manifest code in manifest .xml file but it gives error at super.onReceive(context, intent); – Zeeshan Chaudhry Aug 28 '12 at 10:36
  • I have rechecked complete code. public class NetworkStateReceiver extends BroadcastReceiver is there any thing i have to do in main activity ? – Zeeshan Chaudhry Aug 28 '12 at 10:48
  • No nothing to do in main activity . just declare it in manifest file – Chirag Aug 28 '12 at 10:49
  • 1
    O yes its done Don't know wats the problem have to restart Emulator to make "F8" Work. Thanks alot @Chirag Raval Stay Blessed buddy – Zeeshan Chaudhry Aug 28 '12 at 10:57
  • change super.onReceive(context, intent); to onReceive(context, intent); – raja Jun 04 '13 at 03:45
  • @ChiragRaval could you just delete the line : super.onReceive(context, intent); please ? All errors is this. – alicanbatur Feb 14 '14 at 11:49
  • I've just tested this code and it actually works - about the `super.onReceive(context, intent);` error - I've just commented out this line and its working like it should! @ChiragRaval Thanks for this helpfull answer! – Darko Petkovski Jul 28 '14 at 14:04
  • 3
    `EXTRA_NETWORK_INFO` is deprecated. Here's a workaround http://stackoverflow.com/a/20590138/1939564 – Muhammad Babar Nov 25 '14 at 07:44
  • 2
    From Android 7.0, you can't really register a `BroadcastReceiver` statically that can listen for Connectivity change. – CopsOnRoad Mar 13 '18 at 17:19
5

The code from Chirag Raval above certainly works. The trouble is that the listener will get invoked even when the application is not running in foreground.

IMHO, the better approach is to register / unregister the receiver in the onResume() / onPause() methods of all your application activities. This code should do it:

private final NetworkStateReceiver stateReceiver = new NetworkStateReceiver();

@Override
protected void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    registerReceiver(stateReceiver, filter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(stateReceiver);
}

Obviously, remove the registration from AndroidManifest.xml file.

Using this solution, the receiver will be called also when switching between activities of your application (assuming you are closing them). In such a case, use a static flag (being shared between all your activities) like in the example below (called online):

public class NetworkStateReceiver extends BroadcastReceiver {

    private static boolean online = true;  // we expect the app being online when starting

    public static final String TAG = NetworkStateReceiver.class.getSimpleName();

    public void onReceive(Context context, Intent intent) {
        Log.d(TAG,"Network connectivity change");
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = manager.getActiveNetworkInfo();
        if (ni == null || ni.getState() != NetworkInfo.State.CONNECTED) {
            Log.d(TAG,"There's no network connectivity");
            if (online) // don't show the message if already offline
                Toast.makeText(context, R.string.noInternet, Toast.LENGTH_SHORT).show();
            online = false;
        } else {
            Log.d(TAG,"Network "+ni.getTypeName()+" connected");
            if (!online)  // don't show the message if already online
                Toast.makeText(context, R.string.backOnline, Toast.LENGTH_SHORT).show();
            online = true;
        }
    }
}

If starting your app when being offline, the Toast message will appear; otherwise it appears only when losing / re-establishing the connection .

pepan
  • 669
  • 5
  • 11
  • Would this work when the internet is connected and the strength is weakening(not disconnected) – Kuldeep Bhimte Jul 29 '20 at 06:27
  • @KuldeepBhimte not really; it only works when an event is triggered; check here: https://stackoverflow.com/questions/1206891/android-how-to-monitor-wifi-signal-strength – pepan Aug 06 '20 at 14:56
  • Cool, Worked Like a Charm Seems easier than registering Intent in `AndroidManifest.xml` – Rahul Shyokand Sep 19 '20 at 07:51