How do I disable the Internet connection in Android Emulator?

余生颓废 提交于 2019-11-29 02:06:54

问题


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?


回答1:


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



回答2:


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.




回答3:


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!




回答4:


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.




回答5:


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



回答6:


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.




回答7:


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.




回答8:


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.




回答9:


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.




回答10:


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.




回答11:


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

}


来源:https://stackoverflow.com/questions/8499812/how-do-i-disable-the-internet-connection-in-android-emulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!