Question

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.

Was it helpful?

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;
}

OTHER TIPS

try this

public static boolean isNetworkAvailable(Context ctx) {
    ConnectivityManager connMgr = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    if(connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected() ||
        connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()){

        Log.d("conected","the server is connected");
        Toast.makeText(ctx, "Network is available", Toast.LENGTH_LONG).show();
            return true;
    }
    Log.d("conected","the server is not connected");
    Toast.makeText(ctx, "Network is not available", Toast.LENGTH_LONG).show();

    return false;
 } 

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top