1

is there any way how to take advantage of ContentProvider to check, whether my Android phone is connected to the internet?

Thanks

Waypoint
  • 17,283
  • 39
  • 116
  • 170

4 Answers4

2

I think you want ConnectivityManager. There's an example here.

That explains how to register for notifications. To check directly, you can get a ConnectivityManager instance:

ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = mgr.getActiveNetworkInfo();
boolean is_connected = info.isConnected();
boolean is_available = info.isAvailable();
Community
  • 1
  • 1
dokkaebi
  • 9,004
  • 3
  • 42
  • 60
  • That's probably my preferred way to test for network connectivity. You can keep polling for network connectivity before each call to a web service, and the NetworkInfo.isConnectingOrConnected() method can be quite useful. – Andrew Weir Mar 27 '12 at 10:17
1

this code should help you to query if the data network is available or not:

   public static boolean isDataNetworkAvailable(Context context){
        try{
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null;
        }
        catch (Exception e) { return false; }
    }
waqaslam
  • 67,549
  • 16
  • 165
  • 178
1
// check internet connectivity
public static boolean isNetworkAvailable(Context context) {

    boolean networkStatus;
    try {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
            .getActiveNetworkInfo();

        networkStatus = (activeNetworkInfo != null && activeNetworkInfo.isConnected()) ? true : false;

    } catch (Exception e) {
        e.printStackTrace();
        DinotaLogger.log(e, Level.SEVERE);
        networkStatus = false;
    }

    return networkStatus;
}

Please make sure to put in manifest file. If you are using an emulator to check this, press F8 to disable network access. This works fine with android 2.3.3 emulator.

divix
  • 1,265
  • 13
  • 27
Rakhita
  • 4,493
  • 1
  • 16
  • 15
1

It will tell you in boolean that your android is connected to internet.

I used that:

public boolean isNetworkAvailable() {
            try{
                Context context = getApplicationContext();
                ConnectivityManager connectivity = (ConnectivityManager) 
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
                if (connectivity == null) {

                }else{
                    NetworkInfo[] info = connectivity.getAllNetworkInfo();
                    if (info != null) {
                        for (int i = 0; i < info.length; i++) {
                            if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                                return true;
                            }
                        }
                    }
                }
            }catch (Exception e) {
                System.out.println("error");
            }
            return false;
        }

Also put:

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

this line to your android manifest.

I Used this in my application.

Siten
  • 4,515
  • 9
  • 39
  • 64