简体   繁体   English

Java Android互联网接入:WIFI或3G

[英]Java Android internet Access: WIFI or 3G

In my android application it has to access a remote server to send a request and to receive the response to continue the functionality. 在我的Android应用程序中,它必须访问远程服务器以发送请求并接收响应以继续该功能。

I need to clarify that before send the request, do I have to specially select that it should be WIFI or Mobile(3G) Internet in a situation where both available. 我需要澄清一下,在发送请求之前,我是否必须在两者都可用的情况下专门选择它应该是WIFI或移动(3G)互联网。 I am not sure that ANDROID OS itself will select the best among them Or will give an exception in runtime. 我不确定ANDROID OS本身会选择其中最好的或者会在运行时给出异常。 I can't test it as I am working with emulator. 当我使用模拟器时,我无法测试它。 Would like to know the standard way. 想知道标准方式。

I can check whether it has connected with WIFI or 3G(Mobile) by the following code. 我可以通过以下代码检查它是否已连接WIFI或3G(移动)。 Like to know before I send the request if I have to select that it should be WIFI or MOBILE(3G) internet. 如果我必须选择它应该是WIFI或MOBILE(3G)互联网,我发送请求之前想知道。 A proper guidance is highly appreciated. 非常感谢正确的指导。 Thanks in advance... 提前致谢...

   ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
   NetworkInfo[] netInfo = cm.getAllNetworkInfo();

   for (NetworkInfo ni : netInfo) 
   {
       if (ni.getTypeName().equalsIgnoreCase("WIFI") && ni.isConnected())
       {
            Toast.makeText(getApplicationContext(), "Connected to Internet with WIFI", Toast.LENGTH_LONG).show();
       }
       if (ni.getTypeName().equalsIgnoreCase("MOBILE") && ni.isConnected())
       {
            Toast.makeText(getApplicationContext(), "Connected to Internet with 3G", Toast.LENGTH_LONG).show();
       }
   }

do I have to specially select that it should be WIFI or Mobile(3G) Internet in a situation where both available 在两者都可用的情况下,我是否必须特别选择它应该是WIFI或移动(3G)互联网

The OS handles this. 操作系统处理此问题。 If there is a known and configured WiFi access point available, Android will use it. 如果有可用的已知和配置的WiFi接入点,Android将使用它。

More importantly, you can't "select" it yourself. 更重要的是,你不能自己 “选择”它。 You get what the OS gives you. 你得到了操作系统给你的东西。


The OS handles this. 操作系统处理此问题。 If there is a known and configured WiFi access point available, Android will use it. 如果有可用的已知和配置的WiFi接入点,Android将使用它。

That is true. 那是真实的。 I just wanted to verify this by running your code, so I did this and the results are. 我只是想通过运行你的代码来验证这一点,所以我做了这个,结果是。

Steps: 脚步:

  1. Ensure Wifi is on and connected. 确保Wifi已打开并已连接。
  2. Ensure Mobile(3G) network is on. 确保移动(3G)网络已开启。
  3. Execute your code above 执行上面的代码

Results: android chooses wifi instead of mobile(3g) 结果:android选择wifi而不是移动(3g)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM