3

i have this android application that requires to load data from a remote server via the internet. this update-functionaliy obviously requires the device to not only be connected to some kind of network, but also to the internet.

so: is want to schedule the update-service to some date and when it starts, it should determine whether it actually CAN reach the target server or not. therefore, a simple "is the device connected to wifi?" does not suffice, as the device may be connected to a wireless network that does not offer internet access. something like a PING is required...

whats the easiest / best way to determine, if there is an internet connection is available i.e. server is reachable?

xenonite
  • 1,671
  • 4
  • 28
  • 43

4 Answers4

4
boolean connected;            
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || 
                    connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
                //we are connected to the internet
                connected = true;
            }
            else
                connected = false;

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

james
  • 26,141
  • 19
  • 95
  • 113
  • 2
    problem: what if my device is connected to some wifi network that does NOT function as an internet gateway? – xenonite Dec 02 '10 at 10:38
2

Just try to connect to your server, and timeout if you haven't started download progress in an acceptable period of time.

San Jacinto
  • 8,774
  • 5
  • 43
  • 58
1

Take a look at this answer here on StackOverflow. It should be just what you are looking for.

Community
  • 1
  • 1
Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
0

Here is the best answer to problem:

Device is connected on router but there is no internet gateway active.

Community
  • 1
  • 1
virusss8
  • 235
  • 1
  • 4
  • 19