How to check the real Internet connected to the android?

I am making a program and I have to use wifi to connect to the Internet. I find some information to check if Wi-Fi is connected or not. But in some situations, you can connect a Wi-Fi access point, but you still cannot use the Internet, for example Wi-Fi, the necessary account and password for the certificate in https, or the Wi-Fi AP cannot connect to The internet. So how can I check authentic internet?

+6
source share
3 answers

Just do a β€œPing” on www.google.com, the chances of them down are very low.

PS is what we do in our application.

public static boolean isReachable(Context context) { // First, check we have connectivity final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo netInfo = connMgr.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnected()) { // check if google is reachable try { URL url = new URL("http://www.google.com"); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(10 * 1000); // Ten seconds timeout in milliseconds urlc.connect(); if (urlc.getResponseCode() == 200) { // success return true; } else { // Fail return false; } } catch (IOException e) { Log.e(TAG, e.getMessage()); return false; } } else { return false; } } 
+5
source

See below:

Make a method returning a boolean:

  ConnectivityManager connectivityManager; NetworkInfo wifiInfo, mobileInfo; public Boolean checkNow(Context con){ try{ connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE); wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if(wifiInfo.isConnected() || mobileInfo.isConnected()) { return true; } } catch(Exception e){ System.out.println("CheckConnectivity Exception: " + e.getMessage()); } return false; } 

Use the above method in the onCreate() Like Like method below:

  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); boolean con = checkNow(getApplicationContext()); if(con){ Toast.makeText(getApplicationContext(), "Connection Founded", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getApplicationContext(), "Connection Not Founded", Toast.LENGTH_SHORT).show(); } } 

When you start the application, you will be prompted to specify "Connection Founded" if an Internet connection is available on your device, otherwise, "Connection Not Founded" will be offered.

+2
source

According to the method below, if the device is not in flight mode or there is no network in any connection mode, the method returns false, otherwise it returns true. Do not forget that if the above scripts are executed, it will return null. To do this, it is processed by checking the null netInfo object.

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

}

Remember to get permission to access the network state in the manifest.

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

All Articles