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(); } }
source share