1

is there any broadcast receiver ( or services )that continuously check internet connectivity during the app are stay running. so whenever there are loose connectivity i got some message of loose connectivity. i know ConnectivityManager

private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }

is the class by which we can check that is there connect with internet or not ? but i need to check instantly...

Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
  • 2
    open www.google.com and you can get instant internet connectivity... :) – Murtaza Mar 23 '12 at 09:27
  • ConnectivityManager is probably your best bet, I have tried looking for it before but couldn't find anything. check out this question it has some answers with minor improvements for checking connectivity http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts – Habib Mar 23 '12 at 09:37
  • @user1129443 - Hey relax friends just wanted to make you divert from your stress.. hope you find your solution soon... – Murtaza Mar 23 '12 at 10:41
  • possible duplicate of [How to check if internet connection is present in java?](http://stackoverflow.com/questions/1402005/how-to-check-if-internet-connection-is-present-in-java) – Macarse Apr 11 '12 at 04:03

4 Answers4

1

this try code is working

public static boolean checkConnection(Context mContext) {

        NetworkInfo info = ((ConnectivityManager) mContext
                .getSystemService(Context.CONNECTIVITY_SERVICE))
                .getActiveNetworkInfo();
        if (info == null || !info.isConnected()) {
            return false;
        }
        if (info.isRoaming()) {
            return true;
        }
        return true;
    }
Android
  • 1,417
  • 9
  • 11
0

you can add a broadcast with an specific action:

IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(connectionReceiver, filter);

youchy
  • 427
  • 3
  • 12
  • Yo can define the receiver in the manifest if you want or maybe you want to define the broadcast in the activity that you want. Something like: `private BroadcastReceiver connectionReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON); //DO Something } };` – youchy Mar 23 '12 at 10:24
  • This broadcast advise when the connectivity CHANGE!! it don't receive an intent every N seconds. Maybe you can save the connectivity (on/off) at the start of the app, and then update ir with the receiver. And you will only read the state that you save. – youchy Mar 23 '12 at 10:30
0

use ACCESS_NETWORK_STATE permission and just make a connected method

public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
Android
  • 2,383
  • 1
  • 26
  • 44
0

Untill You are developing an Application that displays network states then try mugging up with ConnectivityManager and other broadcasts.

If the case is (which is mostly usual) you need to do some network based operation like fetching some data from any server.

I don't go for checking network states but rely on UnknownHostException or RuntimeException or etc that get throws when you make a request that tells me "hey network is not available".

This i feel is more better approach as compare to do network check before making api request.

  1. no need of additional code of connectivity manager.

  2. plus what if case developer saves value of connection not available and not updating it and uses it after some time. so its error prone

  3. It will be like a check at exact time when its required and will not effect other operations.

  4. Major one case is that for very positive case where device never looses data connectivity. why to run a piece of code which is of no use.

this is just my preference and i am not questioning the existence of ConnectivityManager :) .

Hope it helps somehow either in this or in some other scene.

Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72