Search code examples
javaandroidwifiwifimanager

How can i check if wifi lost connection ?


I'm new in java and i want to know if my phone lost the wi-fi connection more than 10 sec to send an sms with his GPS information (for an application that make a http page with location and sensor data that the pc request). I just don't know how to proceed, is there an OnStateChange or something ? And how must I implement that code : http://developer.android.com/reference/android/net/NetworkInfo.DetailedState.html for doing that ?

Thanks to reply.


Solution

  • This way:

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