Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporarily disabling internet access in android?

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.

like image 534
Classtronaut Avatar asked Apr 02 '13 05:04

Classtronaut


People also ask

How do I temporarily disable the internet on my phone?

Android Quick SettingsTo open the Quick Settings menu, use two fingers to swipe down from the top of the screen. Tap "Airplane Mode" to toggle this setting or tap a specific network to open its settings. For example, tap the "Wi-Fi" option and then disable Wi-Fi from its settings menu.

Can you disable the internet on an Android phone?

To turn off the Internet connection on an Android phone:Go to Settings > Wireless Network > <bold>Mobile. Simply uncheck the box next to Data Enabled so that your phone will not connect over the data network. (N.B. You will be unable to send or receive MMS when this option is unchecked.)

How do I block the internet on my kids phone?

Prevent web content:Go to Settings > Screen Time. Tap Content & Privacy Restrictions and enter your Screen Time passcode. Tap Content Restrictions, then tap Web Content. Choose Limit Adult Websites, or Allowed Websites Only.


3 Answers

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" />
like image 53
Linga Avatar answered Oct 03 '22 16:10

Linga


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.

like image 42
Siddharth Lele Avatar answered Oct 03 '22 17:10

Siddharth Lele


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)
like image 24
Artem Zinnatullin Avatar answered Oct 03 '22 16:10

Artem Zinnatullin