2

I am new in android testing I am using Robotium for testing purpose and I have a scenario where I need to check is the network (WiFi, 3G, 2G)connection is available or not, so how can i write a test case for this. Please help to solve this.. Thanks.

QED
  • 9,803
  • 7
  • 50
  • 87
user749873
  • 43
  • 1
  • 2
  • 7

1 Answers1

1

Source: How to check internet access on Android? InetAddress never times out

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

    return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}

You'll need these in your manifest

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

I personally had to run a specific check to see if the user could access my server (Possibility of firewall blocking, or the user having only a local connection)

Good Luck!!

Community
  • 1
  • 1
  • it's work fine adding activity instance like this...ConnectivityManager cm = (ConnectivityManager) activity .getSystemService(Context.CONNECTIVITY_SERVICE);! thanks.. – user749873 Mar 13 '12 at 17:48