Android Internet Connection Test

I have the following code that checks to see if there is an Internet connection before calling the AsyncTask method, a β€œTask,” which then extracts information from the Internet. This really really works if the phone is in flight mode or if the phone is not connected to an external wireless Internet, that is, it doesn’t work in its own online store, which comes with a phone plan.

If the phone is connected to an external wireless modem, but the modem is not connected to the Internet, I get power!

if (isOnline()) { new Task().execute(); } else { Toast.makeText(this, "There seems to be no internet access, please try again later!", Toast.LENGTH_LONG).show(); } 

and

  public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { return true; } return false; } 

Does anyone have a β€œproven and true” way around this?

Greetings

Mike.

+4
source share
4 answers

I use to check another condition in If case

  netInfo .isAvailable() 

I think this is the only way we can know if the Internet is available or not.

+8
source

check this piece of code

 try { ConnectivityManager nInfo = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); nInfo.getActiveNetworkInfo().isConnectedOrConnecting(); Log.d(tag, "Net avail:" + nInfo.getActiveNetworkInfo().isConnectedOrConnecting()); ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { Log.d(tag, "Network available"); return true; } else { Log.d(tag, "Network not available"); return false; } } catch (Exception e) { return false; } 

make sure you write the following permission data in android-manifest.xml

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

check the following question for more details. Android network connection

+4
source


No, it does not seem to work correctly with each mode. I tried to use this

 public boolean isOnline() { boolean flag = false; ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); if (cm.getActiveNetworkInfo() != null) { flag = cm.getActiveNetworkInfo().isConnectedOrConnecting(); } return flag; } 
0
source

Check it:

 ConnectivityManager cm,cm1; private static boolean isConnected(Context context) { NetworkInfo networkInfo = null; if (cm != null) { networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); } return networkInfo == null ? false : networkInfo.isConnected(); } private static boolean isConnected1(Context context) { NetworkInfo networkInfo1 = null; if (cm1 != null) { networkInfo1 = cm1.getNetworkInfo(ConnectivityManager.TYPE_WIFI); } return networkInfo1 == null ? false : networkInfo1.isConnected(); } 
0
source

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


All Articles