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
  • 9,403
  • 5
  • 32
  • 45
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

1 Answers1

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
  • 678
  • 6
  • 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