Java-based Internet access: WIFI or 3G

In my Android application, it needs to access a remote server to send a request and receive a response to continue working.

I need to clarify that before sending the request I need to specifically choose that it should be WIFI or Mobile (3G) Internet in a situation where both are available. I'm not sure if ANDROID OS itself will choose the best one or provide an exception at runtime. I can’t verify this because I am working with an emulator. I would like to know a standard way.

I can check if he connected to WIFI or 3G (Mobile) using the following code. Who should know before sending a request if I have to choose that it should be WIFI or MOBILE (3G) Internet. Good leadership is much 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(); } } 
+4
source share
2 answers

I need to specifically choose that it should be WIFI or Mobile (3G) Internet in a situation where both are available

The OS handles this. If there is a known and configured WiFi access point, Android will use it.

More importantly, you cannot “select” it yourself. You get what the OS gives you.

+4
source

The OS handles this. If there is a known and configured WiFi access point, Android will use it.

It's right. I just wanted to test this by running my code, so I did this and the results.

Steps:

  • Make sure Wi-Fi is turned on and connected.
  • Make sure the Mobile (3G) network is turned on.
  • Run the code above

Results: android chooses Wi-Fi instead of mobile (3g)

+2
source

Source: https://habr.com/ru/post/1399765/


All Articles