Question

Is there any better idea how to check if my device is connected to the internet? Or is the only solution use alarm manager or something like that, that checks with the method checking internet connection periodically. But my application is not running, because I need it for updating data for widgets. So my question is: Is there something, that gets response from some java class of android that yes, I am already connected to the internet? Its pretty tough to ask it my words, i hope, you understand me.

Was it helpful?

Solution

public boolean IsConnected() {
    final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
        return true;
    } else {
        return false;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top