8

I want to set up a test to see if my code works properly when there is no internet access. Is there a way to temporarily shut down internet access for a test?

I tried looking around but I couldn't find it.

Classtronaut
  • 203
  • 4
  • 9

3 Answers3

6

This code sample should work for android phones running gingerbread and higher: This enable/disable data connection

private void setMobileDataEnabled(Context context, boolean enabled) {
   final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
  final Class conmanClass = Class.forName(conman.getClass().getName());
  final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
  iConnectivityManagerField.setAccessible(true);
  final Object iConnectivityManager = iConnectivityManagerField.get(conman);
  final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
  final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
  setMobileDataEnabledMethod.setAccessible(true);
  setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}

Don't forget to add this line to your manifest file

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

Also, I know of enabling or disabling wifi:

WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);

where status may be true or false as per requirement.

Also at manifest:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Linga
  • 10,379
  • 10
  • 52
  • 104
  • It does not work for me. I get this error `java.lang.NoSuchMethodException: setMobileDataEnabled [boolean]`. Device: Nexus 5x, Android version: 7.1.1 – David Novák Dec 29 '16 at 13:10
  • @DavidNovák: See here for further explanations: https://stackoverflow.com/questions/26539445/the-setmobiledataenabled-method-is-no-longer-callable-as-of-android-l-and-later?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – ali Mar 27 '18 at 16:23
2

Well, to disable Wifi, you can use this code:

WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true); // "true" TO ENABLE || "false" TO DISABLE

To disable the Data connection, you could use the solution here: https://stackoverflow.com/a/4304110/450534. This method, however, does not work on 2.3+

This answer here has a solution for both 2.3+ and 2.2 and below: https://stackoverflow.com/a/12535246/450534. You could do a simple API check and decide which piece of code to run. Something like this should get you set up:

int currentAPIVersion = android.os.Build.VERSION.SDK_INT;
if (currentAPIVersion >= android.os.Build.VERSION_CODES.GINGERBREAD) {
    // RUN THE CODE FOR 2.3+
} else {
    // RUN THE CODE FOR API LEVEL BELOW 2.3
}

See which of the two works for you. I suspect, it would be the later. I have personally never gotten around to testing the enabling or disabling Data connectivity though.

Props to the authors of the linked solutions. ;-)

NOTE: The Data Connectivity solutions are based on unofficial API's and may not work in future releases.

Community
  • 1
  • 1
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
1

If you are using emulator, you can turn off internet access for it in DDMS settings.

Check out:

  1. How to disable Internet Connection in Android Emulator?
  2. How to disable/enable network, switch to Wifi in Android emulator? (Question about unit testing)
Community
  • 1
  • 1
Artem Zinnatullin
  • 4,305
  • 1
  • 29
  • 43