How do I disable the Internet connection in Android Emulator?
Asked Answered
C

12

72

I am trying to check Internet connectivity on Android using the following method. I have a Wi-Fi connection.

private boolean checkInternetConnection() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    // Test for connection
    if (cm.getActiveNetworkInfo() != null
            && cm.getActiveNetworkInfo().isAvailable()
            && cm.getActiveNetworkInfo().isConnected()) {
        return true;
    }
    else {
        // No conection
        return false;
    }
}

I disconnect my PC from the Internet and then try to run the above method. Still it always returns true!? Why?

Cocytus answered 14/12, 2011 at 5:21 Comment(3)
try this thing on device not on emulator, it will return always uncertain results on emulator on this things...!!Stakhanovism
but i have to show the results on the emulator.. Is there any way i can show the results on emulator ?Cocytus
Using the Pixel_3a_API_30_x86 and Android 11 in the emulator, this seems pretty much impossible. Wifi turns on again when turned off and airplaine mode makes no difference. What I ended up resorting to was changing URLs or disabling internet connection on my computer.Filbert
S
86

From Eclipse

To disable the Internet connection, go to Eclipse and do

menu Window -> Show view -> Other -> Android -> Devices

Now select your running emulator and again go to:

menu Window -> Show View -> Other -> Android -> Emulator Control

Now in that... set Data to Unregister from Home.

From Device and Emulator

On the device or in the emulator, go to:

Setting -> Wireless & networks -> Airplane Mode -> OK
Sera answered 14/12, 2011 at 5:33 Comment(8)
@user370305 yes ofcaurse..that's from the emulator and device.. okay let me add that too in the answer.Sera
Ok... I tried all possible options. isAvailable in code returns false. Yet when my service attempts to connect to remote site, it still succeeds and completes the download. Possibly a bug in the emulator?Server
For me the From Device and Emulator option also prevented Android Studio studio from being able to connect to the emulator, so wasn't all that great for debugging!Spitsbergen
Annoyingly, it is still true that setting a Google Android emulator to Airplane mode prevents debuggers running on the PC from connecting to the emulated device! (In my case, Xamarin Studio is the IDE)Alsworth
That simulates having no connection to a network, but it's also possible to be connected to a network that has no internet access.Anonymous
it's funny: if you turn off wifi on the emulator and you set the emulator's "data" option to "unregistered" (or Denied) and "signal strength" to "None", the app still gets connectivity! /amazing-internet-magicUnlive
even in airplane mode, wifi is still enabled and the device still has internet connectionBelabor
Yeah agreed I'm working on error checking for my app and I cannot hit any breakpoints while the device does not have an internet connection. Really frustrating if you just want to follow. It would be much better if you can debug while the emulator does not have an active internet connection to test these scenarios.Keppel
M
31

Try the below command

WIFI:
$ adb shell svc wifi enable
$ adb shell svc wifi disable
MOBILE DATA:
$ adb shell svc data enable
$ adb shell svc data disable
Metallography answered 5/1, 2018 at 7:36 Comment(2)
Turning on and off wifi and mobile data is not the same thing. Sometimes you want to test wifi being on (and maybe connected) but no internet connection.Anonymous
Does not work for me in latest version of Android Studio. After executing adb shell svc wifi disable, wifi is still enabled.Belabor
S
30

You can disable the Internet connection by pressing F8 in any Android emulator. It is a toggle button, so if it doesn't have Internet connection then it will start the Internet connection and if it already has an Internet connection then it will dis-connect it.

Salinasalinas answered 22/1, 2014 at 9:52 Comment(2)
this doesn't allow me to debug anymore - eclipse launches a new emulatr after pressing f8 (even after pressing it twice). This is also the case with other methods like airplane offSensate
it is not working in my case. I am using android studio 3.3Porcelain
Y
28

Android Studio 3.0 Update

Simply drag and open the control center and click on the airplane mode button as what you are doing on your actual device.

Please bear with me for a very slow emulator.

enter image description here

Yellowbird answered 2/11, 2017 at 19:26 Comment(5)
smarter way, works in windows and mac both emulatorsHannie
I need to have a short key for that. Any idea?Airglow
I put it on Airplane mode, and stills getting internet connection.Holmberg
I am still getting internet connection, even in airplane mode, because wifi is still enabled and I can't find any way to disable itBelabor
It disconects from Metro server also.Quinque
T
16

There are many methods. I am explaining two methods here.

  1. Just hit the F8 key to enable or disable the Internet connection.
  2. Just start your emulator, hold on the power button and then select:

Airplane mode Off

It will disconnect your emulator from the Internet. If you want to connect again, just hold on the power button again and select:

Airplane Mode ON

And your emulator's Internet connection will start working!

Tush answered 21/4, 2014 at 6:43 Comment(1)
even in airplane mode, it can still connect to wifi, so internet is still enabled. also, F8 does nothing for meBelabor
D
3

To check on a real device (and also in the emulator), go to:

Settings > Wireless and Networks > Mobile Networks > Data enabled

Uncheck it and it will work.

Dann answered 14/12, 2011 at 5:29 Comment(1)
this only disables mobile data, not wifiBelabor
B
1

Recent emulator versions provide an easier way, from an ADT view:

Window -> Show view -> Other... -> Android -> Emulator Control

Simply change "Data" value from "Telephony Status" and test your application again.

Backache answered 29/8, 2013 at 12:24 Comment(1)
this may have worked back in the days of ADT, but ADT is no longer supported and this doesn't work for meBelabor
V
1

You can actually disable internet connection in the simulator just as you would in a real phone.

  1. Swipe down the screen to see the notification panel.
  2. Tap on the data network icon
  3. Turn off the cellular network.

I just tried it successfully in an android 4.4 image loaded into my simulator.

Villon answered 11/1, 2017 at 5:16 Comment(1)
this only disables mobile data, not wifiBelabor
B
1

None of the other answers to this question worked for me, the only thing that worked was

adb shell -> su -> ifconfig wlan0 down

and you can use ifconfig wlan0 up to bring it back up again. (I had to restart the device, even after executing ifconfig wlan0 up.)

Obviously make sure mobile data is disabled (or device is in flight mode) before trying this.

Thanks a lot to noamtm's answer for the hint!

Belabor answered 3/8, 2021 at 17:4 Comment(0)
E
0

Steps to disable Internet in the Android emulator:

  1. First install the application into the emulator
  2. Long press the power button of your emulator
  3. Choose Airplane Mode

Now your emulator won't accept an Internet connection.

Note: Here after Airplan mode, you can't install and run your app in the emulator, so before putint it into Airplane mode, run your app in your emulator.

For changing to normal once again, long press emulator and choose the same option.

Excellent answered 1/8, 2013 at 5:52 Comment(1)
the device can still connect to wifi, even in airplane modeBelabor
B
0

telnet localhost 5554 (or Android emulator number)

Internet enabled:

gsm data home

Internet disabled:

gsm data unregistered

You can use my library, available on http://www.cristianmarquez.com.ar.

Baculiform answered 31/10, 2013 at 13:38 Comment(2)
The emulator reacts to the command moving gsm status from 'local network' to 'no network available' but the internet connection from the emulator remains the same.Tumid
this only disables mobile data, not wifiBelabor
A
0

My app was built using React Native and what worked for me was running the device in debug mode and disabling the network connection from the throttling dropdown menu within chrome devtools.

Note: this flow disables any XHR calls made directly within the app code. Any requests done through embedded portals/views might still work if the code generating those requests are outside the app's source directory/code.

I used the React Native Debugger standalone app for this and I followed these steps:

  1. Select the terminal running react-native start and open the developer menu by pressing the letter d

Terminal window displaying a 'opening developer menu' message

  1. On the emulator, select Debug from the developer menu

Emulator screenshot displaying the developer menu

  1. Open the React Native Debugger standalone app

  2. On the emulator, close out and relaunch the app to connect with the React Native Debugger. If using non default local dev port/connection values update settings within the React Native Debugger app.

  3. Enable network inspect on the React Native Debugger app by right clicking on the React or Redux devtools windows Context menu displaying additional settings for the React Native Debugger app

  4. Navigate to the Network tab on the chrome devtools window within the React Native Debugger app and disable the network connection from the throttling menu

chrome devtools network tab showing the default connection setting values chrome devtools network tab showing a disabled connection setting and a list of failed networks calls

Abominate answered 25/7, 2023 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.