Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how i can check the internet access internet access or no internet access.not if wifi connected or not

What I have tried:

Java
public boolean checkOnlineState() {
        ConnectivityManager CManager =
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo NInfo = CManager.getActiveNetworkInfo();
        if (NInfo != null && NInfo.isConnectedOrConnecting()) {
            try {
                if (InetAddress.getByName("https://www.google.fr/").isReachable(10000))
                {
                    // host reachable
                    return true;
                }
                else
                {
                    // host not reachable
                    return false;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

the application stopped immediately!! any help ?
Posted
Updated 11-Jul-16 19:02pm
v2

Java
isConnectedOrConnecting()
is not precise enough it should be
Java
isConnected()


See here: Android check internet connection - Stack Overflow[^]
 
Share this answer
 
use this solution code your app not crash:

C#
public boolean checkInternetConnectionALL() {
       ConnectivityManager conmag = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
       // for all active Network connection

       NetworkInfo info = conmag.getActiveNetworkInfo();
       if (info != null && info.isConnected())
           return true;
       else
           Toast.makeText(context.getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();

       return false;
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900