How do I disable the Internet connection in Android Emulator?

AndroidAndroid EmulatorInternet Connection

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

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!

Solution 6 - Android

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.

Solution 7 - Android

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.

Solution 8 - Android

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.

Solution 9 - Android

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!

Solution 10 - Android

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.

Solution 11 - Android

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.

Solution 12 - Android

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

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChandeepView Question on Stackoverflow
Solution 1 - AndroidMKJParekhView Answer on Stackoverflow
Solution 2 - AndroidVigbyorView Answer on Stackoverflow
Solution 3 - Androidthanhbinh84View Answer on Stackoverflow
Solution 4 - AndroidFangmingView Answer on Stackoverflow
Solution 5 - AndroidNaveed AhmadView Answer on Stackoverflow
Solution 6 - AndroidingsaurabhView Answer on Stackoverflow
Solution 7 - AndroidoabarcaView Answer on Stackoverflow
Solution 8 - AndroidfrapenView Answer on Stackoverflow
Solution 9 - AndroidAdam BurleyView Answer on Stackoverflow
Solution 10 - AndroidsravanView Answer on Stackoverflow
Solution 11 - AndroidCristian MarquezView Answer on Stackoverflow
Solution 12 - AndroidnithinView Answer on Stackoverflow